diff --git a/documentation.html b/documentation.html index 9767498..43ba773 100644 --- a/documentation.html +++ b/documentation.html @@ -32,125 +32,199 @@
Function | Description + |
- | |
abs | Absolute value - |
acos | Arc-cosine - |
add | Pointwise sum x+y - |
addeq | Pointwise sum x+=y - |
all | All the components of x are true - |
and | Pointwise x && y - |
andeq | Pointwise x &= y - |
any | One or more of the components of x are true - |
asin | Arc-sine - |
atan | Arc-tangeant - |
atan2 | Arc-tangeant (two parameters) - |
band | Pointwise x & y - |
bench | Benchmarking routine - |
bnot | Binary negation ~x - |
bor | Binary or x|y - |
bxor | Binary xor x^y - |
ccsDim | Dimensions of sparse matrix - |
ccsDot | Sparse matrix-matrix product - |
ccsFull | Convert sparse to full - |
ccsGather | Gather entries of sparse matrix - |
ccsGetBlock | Get rows/columns of sparse matrix - |
ccsLUP | Compute LUP decomposition of sparse matrix - |
ccsLUPSolve | Solve Ax=b using LUP decomp - |
ccsScatter | Scatter entries of sparse matrix - |
ccsSparse | Convert from full to sparse - |
ccsTSolve | Solve upper/lower triangular system - |
ccs<op> | Supported ops include: add/div/mul/geq/etc... - |
cLU | Coordinate matrix LU decomposition - |
cLUsolve | Coordinate matrix LU solve - |
cdelsq | Coordinate matrix Laplacian - |
cdotMV | Coordinate matrix-vector product - |
ceil | Pointwise Math.ceil(x) - |
cgrid | Coordinate grid for cdelsq + |
add | Pointwise x+y + |
addeq | Pointwise x+=y + |
sub | Pointwise x-y + |
subeq | Pointwise x-=y + |
mul | Pointwise x*y + |
muleq | Pointwise x*=y + |
div | Pointwise x/y + |
diveq | Pointwise x/=y + |
mod | Pointwise x%y + |
modeq | Pointwise x%=y + |
lshift | Pointwise x<<y + |
lshifteq | Pointwise x<<=y + |
rshift | Pointwise x>>y + |
rshifteq | Pointwise x>>=y + |
rrshift | Pointwise x>>>y + |
rrshifteq | Pointwise x>>>=y + |
pow | Pointwise Math.pow(x,y) + |
neg | Pointwise -x + |
abs | Pointwise Math.abs(x) + |
exp | Pointwise Math.exp(x) + |
log | Pointwise Math.log(x) + |
round | Pointwise Math.round(x) + |
sqrt | Pointwise Math.sqrt(x) + |
ceil | Pointwise Math.ceil(x) + |
floor | Poinwise Math.floor(x) + |
isFinite | Pointwise isFinite(x) + |
isNaN | Pointwise isNaN(x) + |
sin | Pointwise Math.sin(x) + |
cos | Pointwise Math.cos(x) + |
tan | Pointwise Math.tan(x) + |
asin | Pointwise Math.asin(x) + |
acos | Pointwise Math.acos(x) + |
atan | Pointwise Math.atan(x) + |
atan2 | Pointwise Math.atan2(x,y) + |
eq | Pointwise x===y + |
neq | Pointwise x!==y + |
lt | Pointwise x<y + |
leq | Pointwise x<=y + |
gt | Pointwise x>y + |
geq | Pointwise x>=y + |
bool | Pointwise !!x + |
not | Pointwise logical negation !x + |
and | Pointwise x&&y + |
andeq | Pointwise x&=y + |
or | Pointwise logical or x||y + |
oreq | Pointwise x|=y + |
xor | Pointwise x^y + |
xoreq | Pointwise x^=y + |
band | Pointwise x&y + |
bnot | Pointwise binary negation ~x + |
bor | Pointwise binary or x|y + |
bxor | Pointwise binary xor x^y + |
when | Pointwise conditional operator x?y:z + |
clip | Pointwise clip to some range + |
saturate | Pointwise clip to the range [0,1] + |
same | Deep comparison of x and y |
clone | Deep copy of Array - |
cos | Pointwise Math.cos(x) - |
det | Determinant - |
diag | Create diagonal matrix - |
dim | Get Array dimensions - |
div | Pointwise x/y - |
diveq | Pointwise x/=y - |
dopri | Numerical integration of ODE using Dormand-Prince RK method. Returns an object Dopri. - |
Dopri.at | Evaluate the ODE solution at a point + |
pointwise | Construct a new pointwise function + + |
+ | |
sum | The sum of an Array + |
prod | The product of an Array + |
inf | The smallest value of an Array + |
sup | The largest value of an Array + |
Function | Description + + |
+ | |
arginf | The index of the smallest value of an Array + |
argsup | The index of the largest value of an Array + |
all | When all the components of an Array are true + |
any | When any of the components of an Array are true + |
mean | The mean of an Array + |
variance | The variance of an Array + |
std | The standard deviation of an Array + |
norm1 | Sum of the magnitudes of an Array + |
norm2 | Square root of the sum of the squares of an Array + |
norm2Squared | Sum of squares of entries of an Array + |
norminf | Largest modulus entry of an Array + |
mapreduce | Construct a new map-reduce function + + |
+ | |
dim | Get Array dimensions + |
empty | Create an empty Array + |
rep | Create an Array by duplicating values + |
zeros | Create an Array of zeros + |
ones | Create an Array of ones + |
range | Create an Array from a range + |
linspace | Generate evenly spaced values + |
logspace | Generate logarithmically spaced values + |
flatten | Flatten Array to a single dimension + |
reshape | Reshape an Array to given dimensions + |
flip | Flip an Array on a given axis + |
fliplr | Flip an Array on the innermost axis + |
flipud | Flip an Array on the second innermost axis + |
rot90 | Rotate an Array 90 degrees counter clockwise + |
roll | Roll an Array on a given axis + |
wrap | Wrap an Array on a given axis + |
concat | Join x and y together on a given axis + |
stack | Join Arrays together on a given axis + |
hstack | Join Arrays on their innermost axis + |
vstack | Join Arrays on their second innermost axis + |
dstack | Join Arrays on their third innermost axis + + |
+ | |
slice | Get a slice of an Array + |
sliceeq | Set the values of a slice of an Array + |
index | Get the values of an Array at a given Array of integer indices + |
indexeq | Set the values of an Array at a given Array of integer indices + |
mask | Get the values in an Array where a boolean Array is true + |
maskeq | Set the values in an Array where a boolean Array is true + |
dot | Matrix-Matrix, Matrix-Vector and Vector-Matrix product + |
det | Determinant |
eig | Eigenvalues and eigenvectors - |
epsilon | 2.220446049250313e-16 - |
eq | Pointwise comparison x === y - |
exp | Pointwise Math.exp(x) - |
floor | Poinwise Math.floor(x) - |
geq | Pointwise x>=y - |
getBlock | Extract a block from a matrix - |
getDiag | Get the diagonal of a matrix - |
gt | Pointwise x>y - |
identity | Identity matrix - |
imageURL | Encode a matrix as an image URL |
inv | Matrix inverse - |
isFinite | Pointwise isFinite(x) - |
isNaN | Pointwise isNaN(x) - |
largeArray | Don't prettyPrint Arrays larger than this - |
leq | Pointwise x<=y - |
linspace | Generate evenly spaced values - |
log | Pointwise Math.log(x) - |
lshift | Pointwise x<<y - |
lshifteq | Pointwise x<<=y - |
lt | Pointwise x<y + |
svd | Singular value decomposition + |
diag | Create diagonal matrix + |
identity | Identity matrix + |
transpose | Matrix transpose + |
inner | Inner product of two Arrays + |
outer | Outer product of two Arrays + |
kron | Kronecker product of two matrices + |
tensor | Tensor product z[i][j] = x[i]*y[j] + |
solve | Solve Ax=b + |
solveLP | Solve a linear programming problem + |
solveQP | Solve a quadratic programming problem |
LU | Dense LU decomposition |
LUsolve | Dense LU solve - |
mapreduce | Make a pointwise map-reduce function - |
mod | Pointwise x%y - |
modeq | Pointwise x%=y - |
mul | Pointwise x*y - |
neg | Pointwise -x - |
neq | Pointwise x!==y - |
norm2 | Square root of the sum of the square of the entries of x - |
norm2Squared | Sum of squares of entries of x - |
norminf | Largest modulus entry of x - |
not | Pointwise logical negation !x - |
or | Pointwise logical or x||y - |
oreq | Pointwise x|=y - |
parseCSV | Parse a CSV file into an Array - |
parseDate | Pointwise parseDate(x) - |
parseFloat | Pointwise parseFloat(x) - |
pointwise | Create a pointwise function - |
pow | Pointwise Math.pow(x) - |
precision | Number of digits to prettyPrint - |
prettyPrint | Pretty-prints x - |
random | Create an Array of random numbers - |
rep | Create an Array by duplicating values + |
Function | Description + |
- | |
round | Pointwise Math.round(x) - |
rrshift | Pointwise x>>>y - |
rrshifteq | Pointwise x>>>=y - |
rshift | Pointwise x>>y - |
rshifteq | Pointwise x>>=y - |
same | x and y are entrywise identical - |
seedrandom | The seedrandom module + |
getDiag | Get the diagonal of a matrix + |
setDiag | Set the diagonal of a matrix + |
getBlock | Get a block from a matrix |
setBlock | Set a block of a matrix - |
sin | Pointwise Math.sin(x) - |
solve | Solve Ax=b - |
solveLP | Solve a linear programming problem - |
solveQP | Solve a quadratic programming problem + + |
+ | |
parseFloat | Pointwise parseFloat(x) + |
parseDate | Pointwise parseDate(x) + |
parseCSV | Parse a CSV file into an Array + |
toCSV | Make a CSV file + |
imageURL | Encode a matrix as an image URL + + |
+ + | |
random | Create an Array of random numbers + |
seedrandom | The seedrandom module + + |
+ | |
ccsSparse | Convert from full to sparse + |
ccsFull | Convert sparse to full + |
ccsDim | Dimensions of sparse matrix + |
ccsDot | Sparse matrix-matrix product + |
ccsGather | Gather entries of sparse matrix + |
ccsScatter | Scatter entries of sparse matrix + |
ccsLUP | Compute LUP decomposition of sparse matrix + |
ccsLUPSolve | Solve Ax=b using LUP decomp + |
ccsTSolve | Solve upper/lower triangular system + |
ccsGetBlock | Get rows/columns of sparse matrix + |
ccs<op> | Supported ops include: add/div/mul/geq/etc... + + |
+ | |
cdotMV | Coordinate matrix-vector product + |
cLU | Coordinate matrix LU decomposition + |
cLUsolve | Coordinate matrix LU solve + |
cgrid | Coordinate grid for cdelsq + |
cdelsq | Coordinate matrix Laplacian + + |
+ | |
dopri | Numerical integration of ODE using Dormand-Prince RK method. Returns an object Dopri. + |
Dopri.at | Evaluate the ODE solution at a point + + |
+ | |
uncmin | Unconstrained optimization + + |
spline | Create a Spline object |
Spline.at | Evaluate the Spline at a point |
Spline.diff | Differentiate the Spline |
Spline.roots | Find all the roots of the Spline - |
sqrt | Pointwise Math.sqrt(x) - |
sub | Pointwise x-y - |
subeq | Pointwise x-=y - |
sum | Sum all the entries of x - |
svd | Singular value decomposition + + |
t | Create a tensor type T (may be complex-valued) |
T.<numericfun> | Supported <numericfun> are: abs, add, cos, diag, div, dot, exp, getBlock, getDiag, inv, log, mul, neg, norm2, setBlock, sin, sub, transpose |
T.conj | Pointwise complex conjugate @@ -164,14 +238,17 @@ |
T.setRow | Set a row |
T.setRows | Set a range of rows |
T.transjugate | The conjugate-transpose of a matrix - |
tan | Pointwise Math.tan(x) - |
tensor | Tensor product ret[i][j] = x[i]*y[j] - |
toCSV | Make a CSV file - |
transpose | Matrix transpose - |
uncmin | Unconstrained optimization + + |
version | Version string for the numeric library - |
xor | Pointwise x^y - |
xoreq | Pointwise x^=y + |
bench | Benchmarking routine + |
epsilon | 2.220446049250313e-16 + |
pi | 3.14159265358979... + |
e | 2.71828182845904... + |
prettyPrint | Pretty-prints x + |
precision | Number of digits to prettyPrint + |
largeArray | Don't prettyPrint Arrays larger than this + |
IN> numeric.exp([1,2]); OUT> [2.718,7.389] @@ -297,8 +375,18 @@+You can use the conditional ternary operator <cond> ? <then> : <else> with the numeric.when() function. + +Math Object functions
OUT> [1,1.414] IN> numeric.tan([1,2]) OUT> [1.557,-2.185] +IN> numeric.bool([1,4,0,-2]) +OUT> [true,true,false,true]
+IN> a = [5,2,3]; b = [1,7,3]; + c = [1,1,1]; d = [2,2,2]; + numeric.when(numeric.lt(a,b),c,d); +OUT> [2,1,2] +
IN> numeric.same([1,2],[1,2]) OUT> true @@ -327,29 +416,28 @@-You can create a multidimensional Array from a given value using numeric.rep() +You can create a multidimensional Array from a given value using numeric.rep(), +or you create Arrays of zeros or ones using the functions numeric.zeros() and +numeric.ones(), and empty Arrays using numeric.empty(). +Utility functions
OUT> false
IN> numeric.rep([3],5) OUT> [5,5,5] IN> numeric.rep([2,3],0) OUT> [[0,0,0], [0,0,0]] -- -You can loop over Arrays as you normally would. However, in order to quickly generate optimized -loops, the numeric library provides a few efficient loop-generation mechanisms. For example, the -numeric.mapreduce() function can be used to make a function that computes the sum of all the -entries of an Array. - -
-IN> sum = numeric.mapreduce('accum += xi','0'); sum([1,2,3]) -OUT> 6 -IN> sum([[1,2,3],[4,5,6]]) -OUT> 21 +IN> numeric.zeros([2,2]) +OUT> [[0,0], + [0,0]] +IN> numeric.ones([5,1]) +OUT> [[1],[1],[1],[1],[1]] +IN> numeric.empty([2,3]) +OUT> [[,,],[,,]]The functions numeric.any() and numeric.all() allow you to check whether any or all entries of an Array are boolean true values. +
IN> numeric.any([false,true]) OUT> true @@ -365,7 +453,80 @@+The functions numeric.inf() and numeric.sup() allow you to get the smallest and largest values +in an Array. You can also use the numeric.argsup() and numeric.arginf() functions to get the +indices of these values. + +Utility functions
OUT> false
+IN> numeric.inf([5.2,6.1,2.8,1.3]) +OUT> 1.3 +IN> numeric.sup([5.2,6.1,2.8,1.3]) +OUT> 6.1 +IN> numeric.argsup([5.2,6.1,2.8,1.3]) +OUT> 1 +IN> numeric.arginf([5.2,6.1,2.8,1.3]) +OUT> 3 ++ +These functions can all be given an optional argument specifying the axis upon which to do the operation. +If a negative index is given it will be used as an offset from the innermost axis. When no axis is given +the operation is performed on a flattened version of the Array. + +
+IN> numeric.sum([[1,2],[3,4],[5,6]], 0) +OUT> [9,12] +IN> numeric.sum([[1,2],[3,4],[5,6]], 1) +OUT> [3,7,11] +IN> numeric.sum( + [[[1,2],[3,4]], + [[5,6],[7,8]]], -1) +OUT> [[ 3, 7], + [11,15]] ++ +You can loop over Arrays as you normally would. However, in order to generate optimized +code, the numeric library provides a few efficient loop-generation mechanisms. For +example, the numeric.mapreduce() function can be used to make a function that +computes the sum of all the entries of an Array. + +It must be provided with two strings. The first is the code to be peformed on each iteration. +It has access to the variables i and x representing the index and Array. +The second string is code to be performed as setup. It is used to construct the base case. +To correctly construct the base case, the s variable contains the dimensions of each +element in x. + +
+IN> sum = numeric.mapreduce('z = numeric.add(z, x[i])','var z = numeric.zeros(s);'); sum([1,2,3]) +OUT> 6 +IN> sum([[1,2,3],[4,5,6]]) +OUT> 21 +IN> sum([[1,2,3],[4,5,6]],0) +OUT> [5,7,9] ++ +The best way to create new pointwise functions is to compose the existing ones, but if for any reason this +wont work you can use numeric.pointwise to generate efficient functions that act like the built-ins. +This ensures they will be fast, and allows them to be combined, composed, and broadcast in the same way. + +To do this you must specify some code acting on the variables x, y, z, +and the corresponding indices i, j, k. Where x and y are +the inputs, and z is the output. For in-place functions x should be assigned to rather than z. +Code for a setup function can also be given, and will be performed before looping. + +The final argument is the type of the pointwise function to generate. This can be either 'binary', +'binary-inplace', 'unary', or 'unary-inplace'. + +
+IN> adddouble = numeric.pointwise('z[k] = 2 * (x[i] + y[j])','','binary'); adddouble([1,2,3],[4,5,6]) +OUT> [10,14,18] +IN> adddouble(5, [10,11,12]) +OUT> [30,32,34] +IN> justdouble = numeric.pointwise('x[i] = 2 * x[i]','','unary-inplace'); v = [1,2,3]; justdouble(v); v; +OUT> [2,4,6] ++ You can create a diagonal matrix using numeric.diag() +
IN> numeric.diag([1,2,3]) OUT> [[1,0,0], @@ -374,6 +535,7 @@The function numeric.identity() returns the identity matrix. +Utility functions
IN> numeric.identity(3) OUT> [[1,0,0], @@ -382,19 +544,35 @@Random Arrays can also be created: -Utility functions
+ ++Kronecker product: +IN> numeric.random([2,3]) -OUT> [[0.05303,0.1537,0.7280], - [0.3839,0.08818,0.6316]] +OUT> [[0.748,0.5187,0.5475], + [0.2482,0.8741,0.4344]]-You can generate a vector of evenly spaced values: +You can generate a vector of evenly or logarithmically spaced values:IN> numeric.linspace(1,5); OUT> [1,2,3,4,5] IN> numeric.linspace(1,3,5); OUT> [1,1.5,2,2.5,3] +IN> numeric.logspace(1,3,5); +OUT> [10,31.62,1e2,316.2,1e3] ++ +Or via some range specifier: + ++IN> numeric.range(7); +OUT> [0,1,2,3,4,5,6] +IN> numeric.range(0, 5); +OUT> [0,1,2,3,4] +IN> numeric.range(1, 3); +OUT> [1,2] +IN> numeric.range(-1, 6, 2); +OUT> [-1, 1, 3, 5]+Slicing
+ +The slice command allows for accessing parts of numeric Arrays in more advanced ways. + +It can be passed an Array of indices for each dimension. + ++IN> numeric.slice([[1,2],[3,4],[5,6]], [2,0]) +OUT> 5 +IN> numeric.slice([[1,2],[3,4],[5,6]], [1]) +OUT> [3,4] ++ +But inside this Array you can also pass in it a slice. This is +a string consisting of three numbers separated by colons start:stop:step +which tell the command to access the Array starting at some index, stopping +at some index, and performing some step size. + ++IN> numeric.slice([0,1,2,3,4,5,6,7,8,9], [1+':'+7+':'+2]) +OUT> [1,3,5] +IN> numeric.slice([1,5,1,7,26], [0+':'+5+':'+2]) +OUT> [1,1,26] ++ +The string is flexible. You can leave out the step section, or +omit the starting and stopping values. In this case the slice starts at the +first element of the Array, and stops at the last. Therefore the string ':' +can be used to specify that every element on some axis be used. + ++IN> numeric.slice([1,5,1,7,26], [1+':'+3]) +OUT> [5,1] +IN> numeric.slice([1,5,1,7,26], [':'+3]) +OUT> [1,5,1] +IN> numeric.slice([1,5,1,7,26], [3+':']) +OUT> [7,26] +IN> numeric.slice([1,5,1,7,26], [':']) +OUT> [1,5,1,7,26] +IN> numeric.slice([[1,2],[3,4],[5,6]], [':',1]) +OUT> [2,4,6] ++ +Negative numbers can also be used. These are assumed to be offsets from +the final element in the Array. So -1 can be used to mean the final element, +-2 the second to last, and so on. + ++IN> numeric.slice([0,1,2,3,4,5,6,7,8,9], [-1]) +OUT> 9 +IN> numeric.slice([0,1,2,3,4,5,6,7,8,9], [-2+':'+10]) +OUT> [8,9] +IN> numeric.slice([0,1,2,3,4,5,6,7,8,9], [-3+':'+3+':'+-1]) +OUT> [7,6,5,4] ++ +The string '|' can be used to insert a new axis of a single element into +an Array. This is particularly useful when used in conjunction with broadcasting +(see below). + ++IN> numeric.slice([0,1,2,3,4,5,6,7,8,9], ['|']) +OUT> [[0,1,2,3,4,5,6,7,8,9]] +IN> numeric.slice([0,1,2,3,4,5,6,7,8,9], [':','|']) +OUT> [[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]] +IN> numeric.dim([0,1,2,3,4,5,6,7,8,9]) +OUT> [10] +IN> numeric.dim(numeric.slice([0,1,2,3,4,5,6,7,8,9], ['|',':'])) +OUT> [1,10] +IN> numeric.dim(numeric.slice([0,1,2,3,4,5,6,7,8,9], [':','|'])) +OUT> [10,1] ++ +The string '...' can be used in the position of the first axis to specify +an access pattern from the right hand side. It corresponds to using ':' +for all axes up until those specified. + ++IN> numeric.slice([[0,1,2],[3,4,5],[6,7,8]], ['...',1]) +OUT> [1,4,7] +IN> numeric.slice([[[0,1],[2,3],[4,5],[6,7],[8,9]]], ['...',1]) +OUT> [[1,3,5,7,9]] +IN> numeric.slice([[[0,1,2],[3,4,5],[6,7,8]]], ['...',1+':']) +OUT> [[[1,2],[4,5],[7,8]]] ++ +As well as getting data you can also set data using the sliceeq function. It takes +as input an Array, a slice Array, and the new values to set. It then sets the values of the +Array in-place. + +If you have previously extracted some data using a slice this can be useful for inserting +it back into an Array. + ++IN> v = [1,2,3,4]; numeric.sliceeq(v,[':'+2],[0,0]); v +OUT> [0,0,3,4] +IN> v = [[1,2],[3,4]]; numeric.sliceeq(v,[':',1],10); v +OUT> [[1,10],[3,10]] ++ + +Broadcasting
+ +When performing pointwise operations between Arrays it is sometimes possible +to calculate a result when the dimensions don't exactly match. This behaviour is +called broadcast. First each Array is wrapped in single element Arrays +until both have the same number of dimensions. Then when performing the operation, +on any dimension with just a single element, the single element in this dimension +will be used as the operand for all calculations along that axis. You can think of +this value as being stretched or repeated, such that it is matched +against every element in the other Array. + ++IN> numeric.mul([[1,2],[3,4],[5,6]], [10,11]) +OUT> [[10,22],[30,44],[50,66]] +IN> numeric.mul([[1,2],[3,4],[5,6]], [[10],[11],[22]]) +OUT> [[10,20],[33,44],[110,132]] +IN> numeric.add(numeric.identity(3), [1,1,1]) +OUT> [[2,1,1], + [1,2,1], + [1,1,2]] +IN> numeric.add(numeric.rep([3,5], 0), [1,2,3,4,5]) +OUT> [[1,2,3,4,5], + [1,2,3,4,5], + [1,2,3,4,5]] +IN> numeric.add(numeric.rep([2,2,2], 0), [[1],[2]]) +OUT> [[[1,1],[2,2]], + [[1,1],[2,2]]] ++ +This can be very useful when used with the new axis '|' slice +string. For example here we can efficiently compute a pairwise multiplication +between two vectors by inserting new single element axis. + ++IN> numeric.mul(numeric.slice([1,2,3],[':','|']), numeric.slice([4,5,6],['|',':'])) +OUT> [[4, 5, 6], + [8, 10,12], + [12,15,18]] ++ +For more information about broadcasting see Numpy's +broadcasting mechanics which numeric attempts to emulate. + +Joining & Reshaping
+ +Arrays can be joined together using the numeric.concat() function. This +takes as input the two arrays to concatenate and the axis upon which to concatenate them. + ++IN> numeric.concat([1,2],[3,4], 0) +OUT> [1,2,3,4] +IN> numeric.concat([[1,2],[3,4]], [[5,6],[7,8]], 0) +OUT> [[1,2],[3,4],[5,6],[7,8]] +IN> numeric.concat([[1,2],[3,4]], [[5,6],[7,8]], 1) +OUT> [[1,2,5,6],[3,4,7,8]] ++ +If you want to concatenate multiple Arrays you can use the numeric.stack() function which +takes as input an Array of Arrays. + +Concatenating on the innermost, second innermost, and third innermost axes, is a very common operation, +so functions for these are provided as numeric.hstack(), numeric.vstack() and +numeric.dstack(). + ++IN> numeric.stack([[1,2],[3,4],[5,6]], 0) +OUT> [1,2,3,4,5,6] +IN> a = [[1,2],[3,4]]; + b = [[5,6],[7,8]]; + numeric.hstack([a,b]) +OUT> [[1,2,5,6], + [3,4,7,8]] +IN> a = [[1,2],[3,4]]; + b = [[5,6],[7,8]]; + numeric.vstack([a,b]) +OUT> [[1,2], + [3,4], + [5,6], + [7,8]] ++ +Arrays can be reshaped to different dimensions using the numeric.reshape() function, +and can be flattened into a single dimension Array using the numeric.flatten() function. + ++IN> numeric.flatten([[1,2],[3,4],[5,6]]) +OUT> [1,2,3,4,5,6] +IN> numeric.reshape([1,2,3,4],[2,2]) +OUT> [[1,2],[3,4]] +IN> numeric.dim([[1,2],[3,4],[5,6]]) +OUT> [3,2] +IN> numeric.reshape([[1,2],[3,4],[5,6]],[2,3]) +OUT> [[1,2,3],[4,5,6]] +IN> numeric.dim([[1,2,3],[4,5,6]]) +OUT> [2,3] ++ +Arrays can be reversed using numeric.flip(), rotated using numeric.rot90(), and rolled using numeric.roll(). + ++IN> numeric.flip([1,2,3,4]) +OUT> [4,3,2,1] +IN> numeric.flip([[1,2],[3,4]], 0) +OUT> [[3,4],[1,2]] +IN> numeric.flip([[1,2],[3,4]], 1) +OUT> [[2,1],[4,3]] +IN> numeric.rot90([[1,2],[3,4]]) +OUT> [[2,4],[1,3]] +IN> numeric.roll([1,2,3,4], 2) +OUT> [3,4,1,2] +IN> numeric.roll([[1,2],[3,4],[5,6]], 1, 0) +OUT> [[5,6],[1,2],[3,4]] ++ +Elements in an Array can be wrapped with a new Array using the numeric.wrap() function. +An optional axis argument can be given to say upon which axis to do the wrapping. + ++IN> numeric.wrap([1,2,3,4]) +OUT> [[1],[2],[3],[4]] +IN> numeric.wrap([[1,2],[3,4],[5,6]]) +OUT> [[[1],[2]],[[3],[4]],[[5],[6]]] +IN> numeric.wrap([1,2,3,4], 0) +OUT> [[1,2,3,4]] +IN> numeric.wrap([[1,2],[3,4],[5,6]], 1) +OUT> [[[1,2]],[[3,4]],[[5,6]]] ++ +Masking & Indexing
+ +Boolean Arrays can be used to mask off certain values in an Array. This is done with the +numeric.mask() function. The first Array is matched against the second boolean Array and those +elements which are true in the boolean Array are returned. Because masking doesn't ensure the dimensions +of the Array will remain valid, the values are returned flattened. + ++IN> a = [[1,5,3,4],[5,9,2,3]]; + numeric.mask(a, numeric.lt(a,5)); +OUT> [1,3,4,2,3] +IN> a = [[1,5,3,4],[5,9,2,3]]; + numeric.mask(a, numeric.gt(a,4)); +OUT> [5,5,9] ++ +You can set the values in an Array with a mask using numeric.maskeq(). +This is useful in conjunction with numeric.mask() function because it allows +different operations to be performed on different parts of an Array given some +condition. For example we can use it to multiply by 10 only when the numbers in the +Array are less than 5. + ++IN> a = [[1,5,3,4],[5,9,2,3]]; + m = numeric.lt(a,5); + b = numeric.mask(a, m); + numeric.maskeq(a,m,numeric.mul(b,10)); a; +OUT> [[10,5,30,40],[5,9,20,30]] ++ +Arrays can also be used as indices to other Arrays. This is similar to specifying +multiple indices at once. It allows you to get specific elements from an Array at once. +To do this the numeric.index() function is used. + ++IN> numeric.index([[1,2],[3,4],[5,6]],[0,2]) +OUT> [[1,2],[5,6]] +IN> numeric.index([[1,2],[3,4],[5,6]],[[0],[1],[1]]) +OUT> [[[1,2]],[[3,4]],[[3,4]]] ++ +To set values at specific indices you can use the numeric.indexeq() function. +This takes as input an Array, an Array of indices, an Array of values, and sets +the contents of the Array in-place. + ++IN> x = [[1,2],[3,4],[5,6]]; numeric.indexeq(x, [0,2], [[0,0],[2,2]]); x; +OUT> [[0,0],[3,4],[2,2]] +Linear algebra
@@ -577,6 +1037,15 @@Linear algebra
[12]]
+IN> numeric.kron([[1,0],[0,1]],[[1,2],[3,4]]) +OUT> [[1,2,0,0], + [3,4,0,0], + [0,0,1,2], + [0,0,3,4]] ++ You can compute the 2-norm of an Array, which is the square root of the sum of the squares of the entries.
IN> numeric.norm2([1,2]) @@ -638,6 +1107,10 @@Complex vectors and matrices can also be handled: diff --git a/src/documentation.html b/src/documentation.html index 9767498..43ba773 100644 --- a/src/documentation.html +++ b/src/documentation.html @@ -32,125 +32,199 @@Complex linear algebra
OUT> {x:0.5588,y:-0.2353} IN> z.sub(w) OUT> {x:1, y:-4} +IN> z.reciprocal() +OUT> {x: 0.12, y: -0.16} +IN> numeric.t(2, 0).reciprocal() +OUT> {x: 0.5, y: 0}
Function | Description + |
- | |
abs | Absolute value - |
acos | Arc-cosine - |
add | Pointwise sum x+y - |
addeq | Pointwise sum x+=y - |
all | All the components of x are true - |
and | Pointwise x && y - |
andeq | Pointwise x &= y - |
any | One or more of the components of x are true - |
asin | Arc-sine - |
atan | Arc-tangeant - |
atan2 | Arc-tangeant (two parameters) - |
band | Pointwise x & y - |
bench | Benchmarking routine - |
bnot | Binary negation ~x - |
bor | Binary or x|y - |
bxor | Binary xor x^y - |
ccsDim | Dimensions of sparse matrix - |
ccsDot | Sparse matrix-matrix product - |
ccsFull | Convert sparse to full - |
ccsGather | Gather entries of sparse matrix - |
ccsGetBlock | Get rows/columns of sparse matrix - |
ccsLUP | Compute LUP decomposition of sparse matrix - |
ccsLUPSolve | Solve Ax=b using LUP decomp - |
ccsScatter | Scatter entries of sparse matrix - |
ccsSparse | Convert from full to sparse - |
ccsTSolve | Solve upper/lower triangular system - |
ccs<op> | Supported ops include: add/div/mul/geq/etc... - |
cLU | Coordinate matrix LU decomposition - |
cLUsolve | Coordinate matrix LU solve - |
cdelsq | Coordinate matrix Laplacian - |
cdotMV | Coordinate matrix-vector product - |
ceil | Pointwise Math.ceil(x) - |
cgrid | Coordinate grid for cdelsq + |
add | Pointwise x+y + |
addeq | Pointwise x+=y + |
sub | Pointwise x-y + |
subeq | Pointwise x-=y + |
mul | Pointwise x*y + |
muleq | Pointwise x*=y + |
div | Pointwise x/y + |
diveq | Pointwise x/=y + |
mod | Pointwise x%y + |
modeq | Pointwise x%=y + |
lshift | Pointwise x<<y + |
lshifteq | Pointwise x<<=y + |
rshift | Pointwise x>>y + |
rshifteq | Pointwise x>>=y + |
rrshift | Pointwise x>>>y + |
rrshifteq | Pointwise x>>>=y + |
pow | Pointwise Math.pow(x,y) + |
neg | Pointwise -x + |
abs | Pointwise Math.abs(x) + |
exp | Pointwise Math.exp(x) + |
log | Pointwise Math.log(x) + |
round | Pointwise Math.round(x) + |
sqrt | Pointwise Math.sqrt(x) + |
ceil | Pointwise Math.ceil(x) + |
floor | Poinwise Math.floor(x) + |
isFinite | Pointwise isFinite(x) + |
isNaN | Pointwise isNaN(x) + |
sin | Pointwise Math.sin(x) + |
cos | Pointwise Math.cos(x) + |
tan | Pointwise Math.tan(x) + |
asin | Pointwise Math.asin(x) + |
acos | Pointwise Math.acos(x) + |
atan | Pointwise Math.atan(x) + |
atan2 | Pointwise Math.atan2(x,y) + |
eq | Pointwise x===y + |
neq | Pointwise x!==y + |
lt | Pointwise x<y + |
leq | Pointwise x<=y + |
gt | Pointwise x>y + |
geq | Pointwise x>=y + |
bool | Pointwise !!x + |
not | Pointwise logical negation !x + |
and | Pointwise x&&y + |
andeq | Pointwise x&=y + |
or | Pointwise logical or x||y + |
oreq | Pointwise x|=y + |
xor | Pointwise x^y + |
xoreq | Pointwise x^=y + |
band | Pointwise x&y + |
bnot | Pointwise binary negation ~x + |
bor | Pointwise binary or x|y + |
bxor | Pointwise binary xor x^y + |
when | Pointwise conditional operator x?y:z + |
clip | Pointwise clip to some range + |
saturate | Pointwise clip to the range [0,1] + |
same | Deep comparison of x and y |
clone | Deep copy of Array - |
cos | Pointwise Math.cos(x) - |
det | Determinant - |
diag | Create diagonal matrix - |
dim | Get Array dimensions - |
div | Pointwise x/y - |
diveq | Pointwise x/=y - |
dopri | Numerical integration of ODE using Dormand-Prince RK method. Returns an object Dopri. - |
Dopri.at | Evaluate the ODE solution at a point + |
pointwise | Construct a new pointwise function + + |
+ | |
sum | The sum of an Array + |
prod | The product of an Array + |
inf | The smallest value of an Array + |
sup | The largest value of an Array + |
Function | Description + + |
+ | |
arginf | The index of the smallest value of an Array + |
argsup | The index of the largest value of an Array + |
all | When all the components of an Array are true + |
any | When any of the components of an Array are true + |
mean | The mean of an Array + |
variance | The variance of an Array + |
std | The standard deviation of an Array + |
norm1 | Sum of the magnitudes of an Array + |
norm2 | Square root of the sum of the squares of an Array + |
norm2Squared | Sum of squares of entries of an Array + |
norminf | Largest modulus entry of an Array + |
mapreduce | Construct a new map-reduce function + + |
+ | |
dim | Get Array dimensions + |
empty | Create an empty Array + |
rep | Create an Array by duplicating values + |
zeros | Create an Array of zeros + |
ones | Create an Array of ones + |
range | Create an Array from a range + |
linspace | Generate evenly spaced values + |
logspace | Generate logarithmically spaced values + |
flatten | Flatten Array to a single dimension + |
reshape | Reshape an Array to given dimensions + |
flip | Flip an Array on a given axis + |
fliplr | Flip an Array on the innermost axis + |
flipud | Flip an Array on the second innermost axis + |
rot90 | Rotate an Array 90 degrees counter clockwise + |
roll | Roll an Array on a given axis + |
wrap | Wrap an Array on a given axis + |
concat | Join x and y together on a given axis + |
stack | Join Arrays together on a given axis + |
hstack | Join Arrays on their innermost axis + |
vstack | Join Arrays on their second innermost axis + |
dstack | Join Arrays on their third innermost axis + + |
+ | |
slice | Get a slice of an Array + |
sliceeq | Set the values of a slice of an Array + |
index | Get the values of an Array at a given Array of integer indices + |
indexeq | Set the values of an Array at a given Array of integer indices + |
mask | Get the values in an Array where a boolean Array is true + |
maskeq | Set the values in an Array where a boolean Array is true + |
dot | Matrix-Matrix, Matrix-Vector and Vector-Matrix product + |
det | Determinant |
eig | Eigenvalues and eigenvectors - |
epsilon | 2.220446049250313e-16 - |
eq | Pointwise comparison x === y - |
exp | Pointwise Math.exp(x) - |
floor | Poinwise Math.floor(x) - |
geq | Pointwise x>=y - |
getBlock | Extract a block from a matrix - |
getDiag | Get the diagonal of a matrix - |
gt | Pointwise x>y - |
identity | Identity matrix - |
imageURL | Encode a matrix as an image URL |
inv | Matrix inverse - |
isFinite | Pointwise isFinite(x) - |
isNaN | Pointwise isNaN(x) - |
largeArray | Don't prettyPrint Arrays larger than this - |
leq | Pointwise x<=y - |
linspace | Generate evenly spaced values - |
log | Pointwise Math.log(x) - |
lshift | Pointwise x<<y - |
lshifteq | Pointwise x<<=y - |
lt | Pointwise x<y + |
svd | Singular value decomposition + |
diag | Create diagonal matrix + |
identity | Identity matrix + |
transpose | Matrix transpose + |
inner | Inner product of two Arrays + |
outer | Outer product of two Arrays + |
kron | Kronecker product of two matrices + |
tensor | Tensor product z[i][j] = x[i]*y[j] + |
solve | Solve Ax=b + |
solveLP | Solve a linear programming problem + |
solveQP | Solve a quadratic programming problem |
LU | Dense LU decomposition |
LUsolve | Dense LU solve - |
mapreduce | Make a pointwise map-reduce function - |
mod | Pointwise x%y - |
modeq | Pointwise x%=y - |
mul | Pointwise x*y - |
neg | Pointwise -x - |
neq | Pointwise x!==y - |
norm2 | Square root of the sum of the square of the entries of x - |
norm2Squared | Sum of squares of entries of x - |
norminf | Largest modulus entry of x - |
not | Pointwise logical negation !x - |
or | Pointwise logical or x||y - |
oreq | Pointwise x|=y - |
parseCSV | Parse a CSV file into an Array - |
parseDate | Pointwise parseDate(x) - |
parseFloat | Pointwise parseFloat(x) - |
pointwise | Create a pointwise function - |
pow | Pointwise Math.pow(x) - |
precision | Number of digits to prettyPrint - |
prettyPrint | Pretty-prints x - |
random | Create an Array of random numbers - |
rep | Create an Array by duplicating values + |
Function | Description + |
- | |
round | Pointwise Math.round(x) - |
rrshift | Pointwise x>>>y - |
rrshifteq | Pointwise x>>>=y - |
rshift | Pointwise x>>y - |
rshifteq | Pointwise x>>=y - |
same | x and y are entrywise identical - |
seedrandom | The seedrandom module + |
getDiag | Get the diagonal of a matrix + |
setDiag | Set the diagonal of a matrix + |
getBlock | Get a block from a matrix |
setBlock | Set a block of a matrix - |
sin | Pointwise Math.sin(x) - |
solve | Solve Ax=b - |
solveLP | Solve a linear programming problem - |
solveQP | Solve a quadratic programming problem + + |
+ | |
parseFloat | Pointwise parseFloat(x) + |
parseDate | Pointwise parseDate(x) + |
parseCSV | Parse a CSV file into an Array + |
toCSV | Make a CSV file + |
imageURL | Encode a matrix as an image URL + + |
+ + | |
random | Create an Array of random numbers + |
seedrandom | The seedrandom module + + |
+ | |
ccsSparse | Convert from full to sparse + |
ccsFull | Convert sparse to full + |
ccsDim | Dimensions of sparse matrix + |
ccsDot | Sparse matrix-matrix product + |
ccsGather | Gather entries of sparse matrix + |
ccsScatter | Scatter entries of sparse matrix + |
ccsLUP | Compute LUP decomposition of sparse matrix + |
ccsLUPSolve | Solve Ax=b using LUP decomp + |
ccsTSolve | Solve upper/lower triangular system + |
ccsGetBlock | Get rows/columns of sparse matrix + |
ccs<op> | Supported ops include: add/div/mul/geq/etc... + + |
+ | |
cdotMV | Coordinate matrix-vector product + |
cLU | Coordinate matrix LU decomposition + |
cLUsolve | Coordinate matrix LU solve + |
cgrid | Coordinate grid for cdelsq + |
cdelsq | Coordinate matrix Laplacian + + |
+ | |
dopri | Numerical integration of ODE using Dormand-Prince RK method. Returns an object Dopri. + |
Dopri.at | Evaluate the ODE solution at a point + + |
+ | |
uncmin | Unconstrained optimization + + |
spline | Create a Spline object |
Spline.at | Evaluate the Spline at a point |
Spline.diff | Differentiate the Spline |
Spline.roots | Find all the roots of the Spline - |
sqrt | Pointwise Math.sqrt(x) - |
sub | Pointwise x-y - |
subeq | Pointwise x-=y - |
sum | Sum all the entries of x - |
svd | Singular value decomposition + + |
t | Create a tensor type T (may be complex-valued) |
T.<numericfun> | Supported <numericfun> are: abs, add, cos, diag, div, dot, exp, getBlock, getDiag, inv, log, mul, neg, norm2, setBlock, sin, sub, transpose |
T.conj | Pointwise complex conjugate @@ -164,14 +238,17 @@ |
T.setRow | Set a row |
T.setRows | Set a range of rows |
T.transjugate | The conjugate-transpose of a matrix - |
tan | Pointwise Math.tan(x) - |
tensor | Tensor product ret[i][j] = x[i]*y[j] - |
toCSV | Make a CSV file - |
transpose | Matrix transpose - |
uncmin | Unconstrained optimization + + |
version | Version string for the numeric library - |
xor | Pointwise x^y - |
xoreq | Pointwise x^=y + |
bench | Benchmarking routine + |
epsilon | 2.220446049250313e-16 + |
pi | 3.14159265358979... + |
e | 2.71828182845904... + |
prettyPrint | Pretty-prints x + |
precision | Number of digits to prettyPrint + |
largeArray | Don't prettyPrint Arrays larger than this + |
IN> numeric.exp([1,2]); OUT> [2.718,7.389] @@ -297,8 +375,18 @@+You can use the conditional ternary operator <cond> ? <then> : <else> with the numeric.when() function. + +Math Object functions
OUT> [1,1.414] IN> numeric.tan([1,2]) OUT> [1.557,-2.185] +IN> numeric.bool([1,4,0,-2]) +OUT> [true,true,false,true]
+IN> a = [5,2,3]; b = [1,7,3]; + c = [1,1,1]; d = [2,2,2]; + numeric.when(numeric.lt(a,b),c,d); +OUT> [2,1,2] +
IN> numeric.same([1,2],[1,2]) OUT> true @@ -327,29 +416,28 @@-You can create a multidimensional Array from a given value using numeric.rep() +You can create a multidimensional Array from a given value using numeric.rep(), +or you create Arrays of zeros or ones using the functions numeric.zeros() and +numeric.ones(), and empty Arrays using numeric.empty(). +Utility functions
OUT> false
IN> numeric.rep([3],5) OUT> [5,5,5] IN> numeric.rep([2,3],0) OUT> [[0,0,0], [0,0,0]] -- -You can loop over Arrays as you normally would. However, in order to quickly generate optimized -loops, the numeric library provides a few efficient loop-generation mechanisms. For example, the -numeric.mapreduce() function can be used to make a function that computes the sum of all the -entries of an Array. - -
-IN> sum = numeric.mapreduce('accum += xi','0'); sum([1,2,3]) -OUT> 6 -IN> sum([[1,2,3],[4,5,6]]) -OUT> 21 +IN> numeric.zeros([2,2]) +OUT> [[0,0], + [0,0]] +IN> numeric.ones([5,1]) +OUT> [[1],[1],[1],[1],[1]] +IN> numeric.empty([2,3]) +OUT> [[,,],[,,]]The functions numeric.any() and numeric.all() allow you to check whether any or all entries of an Array are boolean true values. +
IN> numeric.any([false,true]) OUT> true @@ -365,7 +453,80 @@+The functions numeric.inf() and numeric.sup() allow you to get the smallest and largest values +in an Array. You can also use the numeric.argsup() and numeric.arginf() functions to get the +indices of these values. + +Utility functions
OUT> false
+IN> numeric.inf([5.2,6.1,2.8,1.3]) +OUT> 1.3 +IN> numeric.sup([5.2,6.1,2.8,1.3]) +OUT> 6.1 +IN> numeric.argsup([5.2,6.1,2.8,1.3]) +OUT> 1 +IN> numeric.arginf([5.2,6.1,2.8,1.3]) +OUT> 3 ++ +These functions can all be given an optional argument specifying the axis upon which to do the operation. +If a negative index is given it will be used as an offset from the innermost axis. When no axis is given +the operation is performed on a flattened version of the Array. + +
+IN> numeric.sum([[1,2],[3,4],[5,6]], 0) +OUT> [9,12] +IN> numeric.sum([[1,2],[3,4],[5,6]], 1) +OUT> [3,7,11] +IN> numeric.sum( + [[[1,2],[3,4]], + [[5,6],[7,8]]], -1) +OUT> [[ 3, 7], + [11,15]] ++ +You can loop over Arrays as you normally would. However, in order to generate optimized +code, the numeric library provides a few efficient loop-generation mechanisms. For +example, the numeric.mapreduce() function can be used to make a function that +computes the sum of all the entries of an Array. + +It must be provided with two strings. The first is the code to be peformed on each iteration. +It has access to the variables i and x representing the index and Array. +The second string is code to be performed as setup. It is used to construct the base case. +To correctly construct the base case, the s variable contains the dimensions of each +element in x. + +
+IN> sum = numeric.mapreduce('z = numeric.add(z, x[i])','var z = numeric.zeros(s);'); sum([1,2,3]) +OUT> 6 +IN> sum([[1,2,3],[4,5,6]]) +OUT> 21 +IN> sum([[1,2,3],[4,5,6]],0) +OUT> [5,7,9] ++ +The best way to create new pointwise functions is to compose the existing ones, but if for any reason this +wont work you can use numeric.pointwise to generate efficient functions that act like the built-ins. +This ensures they will be fast, and allows them to be combined, composed, and broadcast in the same way. + +To do this you must specify some code acting on the variables x, y, z, +and the corresponding indices i, j, k. Where x and y are +the inputs, and z is the output. For in-place functions x should be assigned to rather than z. +Code for a setup function can also be given, and will be performed before looping. + +The final argument is the type of the pointwise function to generate. This can be either 'binary', +'binary-inplace', 'unary', or 'unary-inplace'. + +
+IN> adddouble = numeric.pointwise('z[k] = 2 * (x[i] + y[j])','','binary'); adddouble([1,2,3],[4,5,6]) +OUT> [10,14,18] +IN> adddouble(5, [10,11,12]) +OUT> [30,32,34] +IN> justdouble = numeric.pointwise('x[i] = 2 * x[i]','','unary-inplace'); v = [1,2,3]; justdouble(v); v; +OUT> [2,4,6] ++ You can create a diagonal matrix using numeric.diag() +
IN> numeric.diag([1,2,3]) OUT> [[1,0,0], @@ -374,6 +535,7 @@The function numeric.identity() returns the identity matrix. +Utility functions
IN> numeric.identity(3) OUT> [[1,0,0], @@ -382,19 +544,35 @@Random Arrays can also be created: -Utility functions
+ ++Kronecker product: +IN> numeric.random([2,3]) -OUT> [[0.05303,0.1537,0.7280], - [0.3839,0.08818,0.6316]] +OUT> [[0.748,0.5187,0.5475], + [0.2482,0.8741,0.4344]]-You can generate a vector of evenly spaced values: +You can generate a vector of evenly or logarithmically spaced values:IN> numeric.linspace(1,5); OUT> [1,2,3,4,5] IN> numeric.linspace(1,3,5); OUT> [1,1.5,2,2.5,3] +IN> numeric.logspace(1,3,5); +OUT> [10,31.62,1e2,316.2,1e3] ++ +Or via some range specifier: + ++IN> numeric.range(7); +OUT> [0,1,2,3,4,5,6] +IN> numeric.range(0, 5); +OUT> [0,1,2,3,4] +IN> numeric.range(1, 3); +OUT> [1,2] +IN> numeric.range(-1, 6, 2); +OUT> [-1, 1, 3, 5]+Slicing
+ +The slice command allows for accessing parts of numeric Arrays in more advanced ways. + +It can be passed an Array of indices for each dimension. + ++IN> numeric.slice([[1,2],[3,4],[5,6]], [2,0]) +OUT> 5 +IN> numeric.slice([[1,2],[3,4],[5,6]], [1]) +OUT> [3,4] ++ +But inside this Array you can also pass in it a slice. This is +a string consisting of three numbers separated by colons start:stop:step +which tell the command to access the Array starting at some index, stopping +at some index, and performing some step size. + ++IN> numeric.slice([0,1,2,3,4,5,6,7,8,9], [1+':'+7+':'+2]) +OUT> [1,3,5] +IN> numeric.slice([1,5,1,7,26], [0+':'+5+':'+2]) +OUT> [1,1,26] ++ +The string is flexible. You can leave out the step section, or +omit the starting and stopping values. In this case the slice starts at the +first element of the Array, and stops at the last. Therefore the string ':' +can be used to specify that every element on some axis be used. + ++IN> numeric.slice([1,5,1,7,26], [1+':'+3]) +OUT> [5,1] +IN> numeric.slice([1,5,1,7,26], [':'+3]) +OUT> [1,5,1] +IN> numeric.slice([1,5,1,7,26], [3+':']) +OUT> [7,26] +IN> numeric.slice([1,5,1,7,26], [':']) +OUT> [1,5,1,7,26] +IN> numeric.slice([[1,2],[3,4],[5,6]], [':',1]) +OUT> [2,4,6] ++ +Negative numbers can also be used. These are assumed to be offsets from +the final element in the Array. So -1 can be used to mean the final element, +-2 the second to last, and so on. + ++IN> numeric.slice([0,1,2,3,4,5,6,7,8,9], [-1]) +OUT> 9 +IN> numeric.slice([0,1,2,3,4,5,6,7,8,9], [-2+':'+10]) +OUT> [8,9] +IN> numeric.slice([0,1,2,3,4,5,6,7,8,9], [-3+':'+3+':'+-1]) +OUT> [7,6,5,4] ++ +The string '|' can be used to insert a new axis of a single element into +an Array. This is particularly useful when used in conjunction with broadcasting +(see below). + ++IN> numeric.slice([0,1,2,3,4,5,6,7,8,9], ['|']) +OUT> [[0,1,2,3,4,5,6,7,8,9]] +IN> numeric.slice([0,1,2,3,4,5,6,7,8,9], [':','|']) +OUT> [[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]] +IN> numeric.dim([0,1,2,3,4,5,6,7,8,9]) +OUT> [10] +IN> numeric.dim(numeric.slice([0,1,2,3,4,5,6,7,8,9], ['|',':'])) +OUT> [1,10] +IN> numeric.dim(numeric.slice([0,1,2,3,4,5,6,7,8,9], [':','|'])) +OUT> [10,1] ++ +The string '...' can be used in the position of the first axis to specify +an access pattern from the right hand side. It corresponds to using ':' +for all axes up until those specified. + ++IN> numeric.slice([[0,1,2],[3,4,5],[6,7,8]], ['...',1]) +OUT> [1,4,7] +IN> numeric.slice([[[0,1],[2,3],[4,5],[6,7],[8,9]]], ['...',1]) +OUT> [[1,3,5,7,9]] +IN> numeric.slice([[[0,1,2],[3,4,5],[6,7,8]]], ['...',1+':']) +OUT> [[[1,2],[4,5],[7,8]]] ++ +As well as getting data you can also set data using the sliceeq function. It takes +as input an Array, a slice Array, and the new values to set. It then sets the values of the +Array in-place. + +If you have previously extracted some data using a slice this can be useful for inserting +it back into an Array. + ++IN> v = [1,2,3,4]; numeric.sliceeq(v,[':'+2],[0,0]); v +OUT> [0,0,3,4] +IN> v = [[1,2],[3,4]]; numeric.sliceeq(v,[':',1],10); v +OUT> [[1,10],[3,10]] ++ + +Broadcasting
+ +When performing pointwise operations between Arrays it is sometimes possible +to calculate a result when the dimensions don't exactly match. This behaviour is +called broadcast. First each Array is wrapped in single element Arrays +until both have the same number of dimensions. Then when performing the operation, +on any dimension with just a single element, the single element in this dimension +will be used as the operand for all calculations along that axis. You can think of +this value as being stretched or repeated, such that it is matched +against every element in the other Array. + ++IN> numeric.mul([[1,2],[3,4],[5,6]], [10,11]) +OUT> [[10,22],[30,44],[50,66]] +IN> numeric.mul([[1,2],[3,4],[5,6]], [[10],[11],[22]]) +OUT> [[10,20],[33,44],[110,132]] +IN> numeric.add(numeric.identity(3), [1,1,1]) +OUT> [[2,1,1], + [1,2,1], + [1,1,2]] +IN> numeric.add(numeric.rep([3,5], 0), [1,2,3,4,5]) +OUT> [[1,2,3,4,5], + [1,2,3,4,5], + [1,2,3,4,5]] +IN> numeric.add(numeric.rep([2,2,2], 0), [[1],[2]]) +OUT> [[[1,1],[2,2]], + [[1,1],[2,2]]] ++ +This can be very useful when used with the new axis '|' slice +string. For example here we can efficiently compute a pairwise multiplication +between two vectors by inserting new single element axis. + ++IN> numeric.mul(numeric.slice([1,2,3],[':','|']), numeric.slice([4,5,6],['|',':'])) +OUT> [[4, 5, 6], + [8, 10,12], + [12,15,18]] ++ +For more information about broadcasting see Numpy's +broadcasting mechanics which numeric attempts to emulate. + +Joining & Reshaping
+ +Arrays can be joined together using the numeric.concat() function. This +takes as input the two arrays to concatenate and the axis upon which to concatenate them. + ++IN> numeric.concat([1,2],[3,4], 0) +OUT> [1,2,3,4] +IN> numeric.concat([[1,2],[3,4]], [[5,6],[7,8]], 0) +OUT> [[1,2],[3,4],[5,6],[7,8]] +IN> numeric.concat([[1,2],[3,4]], [[5,6],[7,8]], 1) +OUT> [[1,2,5,6],[3,4,7,8]] ++ +If you want to concatenate multiple Arrays you can use the numeric.stack() function which +takes as input an Array of Arrays. + +Concatenating on the innermost, second innermost, and third innermost axes, is a very common operation, +so functions for these are provided as numeric.hstack(), numeric.vstack() and +numeric.dstack(). + ++IN> numeric.stack([[1,2],[3,4],[5,6]], 0) +OUT> [1,2,3,4,5,6] +IN> a = [[1,2],[3,4]]; + b = [[5,6],[7,8]]; + numeric.hstack([a,b]) +OUT> [[1,2,5,6], + [3,4,7,8]] +IN> a = [[1,2],[3,4]]; + b = [[5,6],[7,8]]; + numeric.vstack([a,b]) +OUT> [[1,2], + [3,4], + [5,6], + [7,8]] ++ +Arrays can be reshaped to different dimensions using the numeric.reshape() function, +and can be flattened into a single dimension Array using the numeric.flatten() function. + ++IN> numeric.flatten([[1,2],[3,4],[5,6]]) +OUT> [1,2,3,4,5,6] +IN> numeric.reshape([1,2,3,4],[2,2]) +OUT> [[1,2],[3,4]] +IN> numeric.dim([[1,2],[3,4],[5,6]]) +OUT> [3,2] +IN> numeric.reshape([[1,2],[3,4],[5,6]],[2,3]) +OUT> [[1,2,3],[4,5,6]] +IN> numeric.dim([[1,2,3],[4,5,6]]) +OUT> [2,3] ++ +Arrays can be reversed using numeric.flip(), rotated using numeric.rot90(), and rolled using numeric.roll(). + ++IN> numeric.flip([1,2,3,4]) +OUT> [4,3,2,1] +IN> numeric.flip([[1,2],[3,4]], 0) +OUT> [[3,4],[1,2]] +IN> numeric.flip([[1,2],[3,4]], 1) +OUT> [[2,1],[4,3]] +IN> numeric.rot90([[1,2],[3,4]]) +OUT> [[2,4],[1,3]] +IN> numeric.roll([1,2,3,4], 2) +OUT> [3,4,1,2] +IN> numeric.roll([[1,2],[3,4],[5,6]], 1, 0) +OUT> [[5,6],[1,2],[3,4]] ++ +Elements in an Array can be wrapped with a new Array using the numeric.wrap() function. +An optional axis argument can be given to say upon which axis to do the wrapping. + ++IN> numeric.wrap([1,2,3,4]) +OUT> [[1],[2],[3],[4]] +IN> numeric.wrap([[1,2],[3,4],[5,6]]) +OUT> [[[1],[2]],[[3],[4]],[[5],[6]]] +IN> numeric.wrap([1,2,3,4], 0) +OUT> [[1,2,3,4]] +IN> numeric.wrap([[1,2],[3,4],[5,6]], 1) +OUT> [[[1,2]],[[3,4]],[[5,6]]] ++ +Masking & Indexing
+ +Boolean Arrays can be used to mask off certain values in an Array. This is done with the +numeric.mask() function. The first Array is matched against the second boolean Array and those +elements which are true in the boolean Array are returned. Because masking doesn't ensure the dimensions +of the Array will remain valid, the values are returned flattened. + ++IN> a = [[1,5,3,4],[5,9,2,3]]; + numeric.mask(a, numeric.lt(a,5)); +OUT> [1,3,4,2,3] +IN> a = [[1,5,3,4],[5,9,2,3]]; + numeric.mask(a, numeric.gt(a,4)); +OUT> [5,5,9] ++ +You can set the values in an Array with a mask using numeric.maskeq(). +This is useful in conjunction with numeric.mask() function because it allows +different operations to be performed on different parts of an Array given some +condition. For example we can use it to multiply by 10 only when the numbers in the +Array are less than 5. + ++IN> a = [[1,5,3,4],[5,9,2,3]]; + m = numeric.lt(a,5); + b = numeric.mask(a, m); + numeric.maskeq(a,m,numeric.mul(b,10)); a; +OUT> [[10,5,30,40],[5,9,20,30]] ++ +Arrays can also be used as indices to other Arrays. This is similar to specifying +multiple indices at once. It allows you to get specific elements from an Array at once. +To do this the numeric.index() function is used. + ++IN> numeric.index([[1,2],[3,4],[5,6]],[0,2]) +OUT> [[1,2],[5,6]] +IN> numeric.index([[1,2],[3,4],[5,6]],[[0],[1],[1]]) +OUT> [[[1,2]],[[3,4]],[[3,4]]] ++ +To set values at specific indices you can use the numeric.indexeq() function. +This takes as input an Array, an Array of indices, an Array of values, and sets +the contents of the Array in-place. + ++IN> x = [[1,2],[3,4],[5,6]]; numeric.indexeq(x, [0,2], [[0,0],[2,2]]); x; +OUT> [[0,0],[3,4],[2,2]] +Linear algebra
@@ -577,6 +1037,15 @@Linear algebra
[12]]
+IN> numeric.kron([[1,0],[0,1]],[[1,2],[3,4]]) +OUT> [[1,2,0,0], + [3,4,0,0], + [0,0,1,2], + [0,0,3,4]] ++ You can compute the 2-norm of an Array, which is the square root of the sum of the squares of the entries.
IN> numeric.norm2([1,2]) @@ -638,6 +1107,10 @@Complex vectors and matrices can also be handled: diff --git a/src/numeric.js b/src/numeric.js index 537b68f..d0c8d42 100644 --- a/src/numeric.js +++ b/src/numeric.js @@ -29,6 +29,7 @@ numeric._myIndexOf = (function _myIndexOf(w) { for(k=0;kComplex linear algebra
OUT> {x:0.5588,y:-0.2353} IN> z.sub(w) OUT> {x:1, y:-4} +IN> z.reciprocal() +OUT> {x: 0.12, y: -0.16} +IN> numeric.t(2, 0).reciprocal() +OUT> {x: 0.5, y: 0}