Ad

You are given a sequence a_n.

The sequences terms are written in terms of two other sequences. a_n progresses as follows:

a_n = [x_1, y_1, x_2, y_2, x_3, y_3, ...]

Decode the sequence and return x_n and y_n as functions in the form [x_n, y_n]

function decodeC(a) {
  return [(n) => a((2*n + 1)), (n)=>a(n*2)]
}
Linear Algebra
Mathematics
Algebra
Matrix

Given a matrix M and a vector v, linearly transform the vector using the matrix.

ex:

const M = [[2,0],[0,2]]
const v = [1,1]
transform(M, v); // [2,2]

More examples:

const M = [[2,2],[3,2]]
const v = [1,1]
transform(M, v); // [5,4]
const M = [[2,0],[0,2]]
const v = [2,3]
transform(M, v); // [4,6]
const M = [[2,5],[0,3]]
const v = [1,2]
transform(M, v); // [5,5]
function transpose(A) {
  let arrs = []
  /*
  |4 5| -> |4 6|
  |6 7|    |5 7|
  [[4,5], [6,7]] -> [[4,6], [5,7]]
  */
  A.forEach((x,i)=>{
    x.forEach((v,i2)=>{
      if(!arrs[i2]){
        arrs[i2] = [v]
      }else arrs[i2].push(v)
    })
  })
  return arrs
}

function scale(v,a) {
let ret = []
v.forEach((x,i)=>{
  ret.push(x*a)
})
return ret
}

function transform(M, a) {
let m = transpose(M)
let ae = []
a.forEach((x,i)=>{
  ae.push(scale(m[i], x))
})
return sum(ae)
}

function sum(v) {
  let s = v[0]
  v.forEach((x,i)=>{
    if(i == 0) return
    x.forEach((y)=>{
      s[i] += y
    })
})
  return s
}