decode - decode an array based on a key
val0.decode(
val1)
The decode method is similar to the APL ⊥
operator
. It can be used to convert a sequence of
digits to an integer in an arbitrary base. It can be used to evaluate
polynomials. And it can be used to convert, for example,
hours/minutes/seconds to seconds in one step.
val0 can be a scalar or vector. val1 can be a vector or multi-dimensional array. Note that the number of rows of the array should match the length of val0. Note also that the first element of a val0 vector is ignored - it is just there for conformance checking.
In the scalar val0 form, a vector of powers of val0 is computed, with the same length as the val1 vector or with the same length as the number of rows of a val1 array. This power vector is then used to form a (+,*) inner product with the whole val1 vector or each column of a val1 array.
In the vector val0 form, the first element is dropped and a 1 is implicitly concatenated. The resulting vector is reversed, a multiplication accum is performed, and the result reversed again. The resulting vector is used to form a (+,*) inner product with the whole val1 vector or each column of a val1 array.
The decode method is conceptually the inverse of the encode method.
The decoded value
ShapeCheck
if val0 and val1 are
not conformal for decode
// Convert 1 2 3 to base 8 8->decode([1,2,3]) 123 // Convert two sets of digits to base 10 10->decode([[1,2,3],[4,5,6]].transpose()) 123 456 // Evaluate 2*x**4 - x + 3 at (x == 5) 5.->decode([2,0,0,-1,3]) 1248. // Convert days/hours/minutes/seconds to seconds [0,24,60,60].decode([3,4,5,6]) 273906 // 86400 seconds in a day [0,24,60,60].decode([1,0,0,0]) 86400 // Show how decode works (conceptually) // Basis is reversed reverse-accum [24,60,60,1].reverse().accum(`*).reverse() 86400 3600 60 1 // Inner product of basis [86400,3600,60,1].inner(`+,`*,[3,4,5,6]) 273906