{"record":{"id":"720dfc7b93b073cb","repo":"lovell/sharp","slug":"expected-a-and-b-to-be-arrays-of-the-same-length","errorCode":null,"errorMessage":"Expected a and b to be arrays of the same length","messagePattern":"Expected a and b to be arrays of the same length","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/operation.mjs","lineNumber":838,"sourceCode":"    this.options.linearA = [];\n  } else if (is.number(a)) {\n    this.options.linearA = [a];\n  } else if (Array.isArray(a) && a.length && a.every(is.number)) {\n    this.options.linearA = a;\n  } else {\n    throw is.invalidParameterError('a', 'number or array of numbers', a);\n  }\n  if (!is.defined(b)) {\n    this.options.linearB = [];\n  } else if (is.number(b)) {\n    this.options.linearB = [b];\n  } else if (Array.isArray(b) && b.length && b.every(is.number)) {\n    this.options.linearB = b;\n  } else {\n    throw is.invalidParameterError('b', 'number or array of numbers', b);\n  }\n  if (this.options.linearA.length !== this.options.linearB.length) {\n    throw new Error('Expected a and b to be arrays of the same length');\n  }\n  return this;\n}\n\n/**\n * Recombine the image with the specified matrix.\n *\n * @since 0.21.1\n *\n * @example\n * sharp(input)\n *   .recomb([\n *    [0.3588, 0.7044, 0.1368],\n *    [0.2990, 0.5870, 0.1140],\n *    [0.2392, 0.4696, 0.0912],\n *   ])\n *   .raw()\n *   .toBuffer(function(err, data, info) {","sourceCodeStart":820,"sourceCodeEnd":856,"githubUrl":"https://github.com/lovell/sharp/blob/56676c69180a32b468123e090f87bfee30539b49/lib/operation.mjs#L820-L856","documentation":"Thrown by linear(a, b) when, after normalization, the arrays built for a and b have different lengths. linear applies the per-channel transform out = a*in + b; a and b must align channel-by-channel. Sharp normalizes scalars to single-element arrays, so a mismatch only arises when one argument is an array and the other is either a scalar or an array of a different length.","triggerScenarios":"linear([1, 1, 1], [0]) — a has 3 entries, b has 1. linear([1.2], [0, 0, 0]) — lengths 1 vs 3. linear([1, 1, 1], 0) is fine (0 becomes [0], length 1 vs 3 -> mismatch, throws). Note: passing a scalar for one side and a multi-element array for the other triggers this because the scalar becomes a length-1 array.","commonSituations":"Applying a per-channel multiplier (3-element array for RGB) but a single scalar bias, forgetting to expand the scalar to a matching array. Mixing array and scalar forms unintentionally. Changing the number of channels mid-pipeline without updating the linear arrays.","solutions":["Make a and b the same length: pass arrays of equal size, or pass scalars for both.","To apply one multiplier and one bias to all channels, pass two scalars: linear(1.2, 0).","To apply per-channel transforms, expand both to the channel count: linear([a1,a2,a3], [b1,b2,b3])."],"exampleFix":"// before\nsharp(img).linear([1.2, 1.0, 0.8], 0)\n\n// after\nsharp(img).linear([1.2, 1.0, 0.8], [0, 0, 0])","handlingStrategy":"validation","validationCode":"function safeLinear(a, b) {\n  const toArr = v => (typeof v === 'number') ? [v] : v;\n  const aa = toArr(a), bb = toArr(b);\n  if (Array.isArray(aa) && Array.isArray(bb) && aa.length !== bb.length) {\n    throw new Error(`linear: a and b length mismatch (${aa.length} vs ${bb.length})`);\n  }\n  return sharp.pipeline ? null : null; // placeholder; use sharp(img).linear(a, b) directly\n}\n// usage: ensure a and b are both scalars, or arrays of equal length","typeGuard":"function linearArgsCompatible(a, b) {\n  const aArr = Array.isArray(a), bArr = Array.isArray(b);\n  if (aArr && bArr) return a.length === b.length;\n  return true; // scalar(s) are normalized internally; mismatch only when both are arrays of differing length, or one array vs one scalar where the array has length != 1\n}","tryCatchPattern":null,"preventionTips":["Pass two scalars for a uniform transform, or two equal-length arrays for per-channel.","Avoid mixing a multi-element array with a single scalar.","When applying per-channel multipliers, expand the bias to the same length."],"tags":["linear","operation","validation","array-length"],"backgroundTag":null,"analyzedSha":"56676c69180a32b468123e090f87bfee30539b49","analyzedAt":"2026-08-13T04:44:31.201Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}