-
Notifications
You must be signed in to change notification settings - Fork 73
BIDMat Quick Reference
Daniel Seita edited this page Feb 8, 2015
·
174 revisions
| Operator | Description |
|---|---|
| a.t | transpose |
| a + b | element-wise addition |
| a - b | element-wise subtraction |
| a / b | element-wise division |
| a * b | matrix multiply |
| a *^ b | matrix multiply w/ transpose a * b.t |
| a ^* b | matrix multiply w/ transpose a.t * b |
| Expression | Description | Return type |
|---|---|---|
| a >, <, >=, <=, ==, != | element-wise comparison | same as input |
| Expression | ASCII alias | Description |
|---|---|---|
| a ∙ b | dot | dot product along columns |
| a ∙→ b | dotr | dot product along rows |
| a ∘ b | *@ | element-wise multiply |
| a ⊗ b | kron | kronecker product |
| a ▷ b | rsolve | inv(a) * b |
| a ◁ b | lsolve | a * inv(b) |
| Expression | Description | Returns |
|---|---|---|
| size(a) | size of the matrix | (nrows, ncols) |
| a.length | number of elements | nrows * ncols |
| a.nrows | number of rows | nrows |
| a.ncols | number of cols | ncols |
| Expression | returns | Description | Type |
|---|---|---|---|
| val a = 1.0\2 on 3\4 |
1 2 3 4 |
Doubles and Ints, cast to FMat |
FMat |
| a(0,1) | 2 | array access | Float |
| a(0,0\1) | 1 2 | IMat column slice | FMat |
| a(?,0) |
1 3 |
wildcard row slice | FMat |
| a(?) |
1 3 2 4 |
linear access column-major order |
FMat |
| Expression | returns | Description | Type |
|---|---|---|---|
| 0 to 2 | Range(0,1,2) | closed range | Range.Inclusive |
| 0 until 2 | Range(0,1) | open range | Range |
| val b = (0 to 3) on (4 until 8) |
0 1 2 3 4 5 6 7 |
Implicit cast to IMat | IMat |
| b(0 until 2, 0) |
1 4 |
range access | FMat |
| b(0 until 3) |
1 4 2 |
linear access (column-major) |
FMat |
| irow(1->4) | 1,2,3 | -> casts to an open range | IMat |
| Function | operation | returns |
|---|---|---|
| sum(a,i) |
sum along index i i=1: columns or i=2: rows |
summed values |
| prod(a,i) |
product along index i i=1: columns or i=2: rows |
product values |
| mean(a,i) |
mean along index i i=1: columns or i=2: rows |
mean values |
| variance(a,i) |
variance along index i i=1: columns or i=2: rows |
variance values |
| maxi(a,i) |
max along index i i=1: columns or i=2: rows |
max values |
| mini(a,i) |
min along index i i=1: columns or i=2: rows |
max values |
| maxi2(a,i) |
max along index i i=1: columns or i=2: rows |
(maxvals, maxinds) |
| mini2(a,i) |
min along index i i=1: columns or i=2: rows |
(minvals, mininds) |
| Function | operation | returns |
|---|---|---|
| sort[down](a) | sort columns ascending [descending] | sorted values |
| sort[down](a,i) |
sort ascending [descending] i=1: columns, i=2: rows |
sorted values |
| sort[down]2(a) | sort columns ascending [descending] | (sortvals, sortinds) |
| sort[down]2(a,i) |
sort ascending [descending] i=1: columns, i=2: rows |
(sortvals, sortinds) |
| sortrows[down](a,i) |
sort rows ascending [descending] i=1: columns, i=2: rows |
(sortvals, sortinds) |
| Expression | Description | Returns |
|---|---|---|
| find(a) | find linear indices of non-zeros of a | linear inds |
| find2(a) | find (row,col) indices of non-zeros of a | (rowinds, colinds) |
| find3(a) | find (row,col) indices and values of non-zeros of a | (rowinds, colinds, values) |
| Expression | Description |
|---|---|
| cumsum(a) | cumulative sum along columns |
| cumsum(a,i) | cumulative sum along dimension i |
| accum(inds, vals, nrows, ncols) | accumulate vals at given indices |
| Expression | Description |
|---|---|
| v = unique(a) | unique values in a |
| (v,i,j) = unique3(a) |
returns unique values v, indices i,j such that a(i) = v, v(j) = a |
| (v,i,j) = uniquerows(a) |
returns unique rows v, indices i,j such that a(i,?) = v, v(j,?) = a |
| Expression | returns | Description | Type |
|---|---|---|---|
| [i,,d]row(2,2,4) | 2,2,4 | row constructor | [I,F,D]Mat |
| [i,,d]col(2,3,4) |
2 3 4 |
column constructor | [I,F,D]Mat |
| [i,,d,g]zeros(2,4) |
0 0 0 0 0 0 0 0 |
Matrix of zeros | [I,F,D,G]Mat |
| [i,,d,g]ones(2,4) |
1 1 1 1 1 1 1 1 |
Matrix of ones | [I,F,D,G]Mat |
| [d,,g]rand(2,2) |
0.324325 0.453237 0.135234 0.348914 |
Random matrix in [0,1] | [D,F,G]Mat |
| Expression | Description | Type |
|---|---|---|
| load[F,D,I,S,SD]Mat(fname) |
loads a matrix from file fname. ".txt" suffix is interpreted as text ".gz" is interpreted as gzip ".lz4" is interpreted as lz4 |
[F,D,I,S,SB,SD]Mat |
| save[F,D,I,S,SD]Mat(fname,a) |
saves matrix "a" to file fname. same suffix rules as above |
[F,D,I,S,SB,SD]Mat |
| loadMat(fname) |
loads a matrix from file fname. reads matrix type from file same suffix rules |
static type Mat [F,D,I,S,SB,SD]Mat |
| saveMat(fname,a) |
saves matrix "a" to file fname. same suffix rules as above |
[F,D,I,S,SB,SD]Mat |
| load(fname,"vname") |
loads variable named "vname" from Matlab v7.3 (HDF5) file |
[F,D,I,S,SD]Mat |
| saveAs(fname,v,"vname") |
saves variable v as "vname" into Matlab v7.3 (HDF5) file |
[F,D,I,S,SD]Mat |
| loadIDX(fname) | loads an IDX file from fname | FND |
| loadLibSVM(fname) | loads file in LibSVM format |
(SMat, IMat, FMat) data,labels,weights |
| Matrix Type | Element Type |
|---|---|
| FMat | Float |
| DMat | Double |
| IMat | Int |
| LMat | Long |
| CMat | Float (representing complex) |
| GMat | Float in GPU mem |
| GIMat | Int in GPU mem |
| Matrix Type | Index Type | Element Type |
|---|---|---|
| SMat | Int | Float |
| SDMat | Int | Double |
| GSMat | Int | Float (GPU mem) |
Conversions
| Function | Arguments | Return Values |
|---|---|---|
| sparse(ii, jj, vv, nrows, ncols) |
ii IMat of row indices jj IMat of col indices vv [F,D]Mat of values (nrows, ncols) matrix dims |
[S,SD]Mat |
| sparse(a) | a [F,D]Mat (nr,nc) | [S,SD]Mat (nr,nc) |
| full(a) | a [S,SD,GS]Mat (nr,nc) | [F,D,G]Mat (nr,nc) |
| contents(a) | a [S,SD,GS]Mat |
[F,D,G]Mat view of values |
| Expression | Description | Returns |
|---|---|---|
| a + b.i |
build complex matrix from FMat a and FMat b |
CMat |
| c.re | real part of CMat c | FMat |
| c.im | imaginary part of CMat c | FMat |
| 3 + 4.i | implicit cast to 1x1 CMat | CMat |
| Expression | Description | Returns |
|---|---|---|
| FND(a) |
construct FND from FMat a |
FND |
| a.dims | get array dimensions | IMat |
| a.toFMat[View](nr,nc) |
convert to FMat [View] with dims (nr,nc) |
FMat |
| f.reshape[View](d1,d2,...) | reshaped FND [View] | FND with dims (d1,d2,...) |
| f.transpose(i1,i2,...) | generalized transpose | FND |
| Expression | Description | Return Type |
|---|---|---|
| loadImage(fname) | load image from file | Image |
| im.toFND |
convert to ND array depth,x,y |
FND |
| im.toFMat |
convert grayscale to FMat x,y |
FND |
| im.toIMat |
convert to ARGB IMat x,y |
FND |
| im.resize(x,y) | scale image to given dims | Image |
| im.scale(f) | scale image using factor f | Image |
| Image(a) |
Construct an image from IMat,FMat,FND |
Image |
| show(a) |
display from Image,IMat,FMat,FND |
Image |
| Expression | returns | Description | Return Type |
|---|---|---|---|
| csrow("you","and","me") | you,and,me | row of strings | CSMat |
| val c = cscol("up","down") |
up down |
column of strings | CSMat |
| c(0,0) | up | array access | String |
SBMat is a sparse matrix of bytes. The columns represent strings, so this format can only support 1D arrays of string. SBMat is much more efficient for I/O than CSMat, but is typically converted to/from CSMat before and after I/O.
| Expression | Description |
|---|---|
| val sb=SBMat(c) | convert CSMat c to SBMat |
| val c=CSMat(sb) | convert SBMat sb to CSMat |
| Expression | Value | Description |
|---|---|---|
| val d=Dict(csrow("A","B")) | Dict | build a dictionary from a CSMat |
| val d=Dict(csm,counts) | Dict | build from CSMat and IMat of counts |
| d("A") | 0 | lookup index of string |
| d(1) | B | retrieve string by index |
| d(c:CSMat) | IMat | bulk lookup of strings |
| d(c:IMat) | CSMat | bulk retrieval of strings |
| d.count("A") | 1 | retrieve count of string |
| d.count(0) | 1 | retrieve count by index |
| d.cstr | CSMat | retrieve the string matrix |
| d.counts | DMat | retrieve the counts matrix |
| union(d1,d2) | d3(Dict) | returns the union of two dictionaries |
| d1 --> d2 | map(IMat) | mapping from indices in d1 to d2 |
| Expression | Description | Type |
|---|---|---|
| val a = b | value (constant) assignment | type(b) |
| var a = b | variable assignment | type(b) |
| a <-- b | copy contents of b into a | type(b) or GPU/CPU counterpart |
| a ~ b op c |
contents placed in a without copy operator ~ has precedence over op only use for binary operations |
type(a) = type(b op c) |
| Expression | Caching behavior |
|---|---|
| val a = expr | expr evaluated and cached, reference placed in a |
| var a = expr | expr evaluated and cached, reference placed in a |
| a <-- expr | expr evaluated and cached, copied to a |
| a ~ b op c | no interaction with cache |
| Expression | Description | Inputs | Outputs |
|---|---|---|---|
| tic | start a seconds timer | none | none |
| toc | number of seconds since tic | none | Float |
| flip |
reset seconds and flops counters |
none | none |
| [g]flop |
return [g]flops and time since flip |
none | (Float, Float) |
| max(a,b[,c]) | element-wise max |
[D,I,F,G]Mat or constant |
[D,I,F,G]Mat save into c |
| min(a,b[,c]) | element-wise min |
[D,I,F,G]Mat or constant |
[D,I,F,G]Mat save into c |
| invperm(p) | inverse of a permutation | IMat | IMat |
| norm(m) | matrix (Frobenius) norm | [D,F]Mat | Double |
| getdiag(m) |
get the leading diagonal of m as a vector |
[D,I,F,G]Mat | [D,I,F,G]Mat |
| mkdiag(v) |
return square matrix with vector v on leading diagonal |
[D,I,F,G]Mat | [D,I,F,G]Mat |
| Expression | Description | Args | Returns |
|---|---|---|---|
| setseed(m) | set the random seed | m:Int | Unit |
| randperm(n) |
random permutation of 0,...,n-1 |
n:Int | IMat |
| [d,,g]rand(m,n) |
mxn matrix of uniform in [0,1] random values |
m,n Int | [D,F,G]Mat |
| [d,,g]rand(m,n,minv,maxv) |
mxn matrix of uniform in [minv,maxv] random values |
m,n Int minv, maxv Float |
[D,F,G]Mat |
| [c,,d,g]normrnd(mu,sig,m,n) |
mxn matrix of normal values, mean mu, sdev sig |
mu,sig Float m,n Int |
[C,F,D,G]Mat |
| [,d]gamrnd(sh,sc,m,n) |
mxn gamma random values, shape sh, scale sc |
sh,sc Float m,n Int |
[F,D]Mat |
| [,d]laprnd(a,b,m,n) |
mxn laplace random values, params a, b |
a,b Float m,n Int |
[F,D]Mat |
| [,d]cauchyrnd(a,b,m,n) |
mxn cauchy random values, params a,b |
a,b Float m,n Int |
[F,D]Mat |
| [,d]exprnd(a,b,m,n) |
mxn exponential random values, params a,b |
a,b Float m,n Int |
[F,D]Mat |
| [,d]betarnd(p,q,m,n) |
mxn beta random values, params p,q |
p,q Float m,n Int |
[F,D]Mat |
| poissrnd(lambda,m,n) |
mxn poisson random values, rate lambda |
lambda FMat m,n Int |
IMat |
| binornd(k,p,m,n) |
mxn binomial random values, p=prob, k reps |
p:Double, k:Int m,n Int |
IMat |
| bernrnd(p,m,n) |
mxn bernoulli random values, p=prob |
p:Double m,n Int |
IMat |
| geornd(p,m,n) |
mxn geometric random values, p=prob |
p:Double m,n Int |
IMat |
| nbinornd(k,p,m,n) |
mxn neg. binomial random values, p=prob, k reps |
p:Double, k:Int m,n Int |
IMat |
| Expression | Description |
|---|---|
| resetGPU[s] | Reset current[all] GPU[s] |
| GPUmem | return GPU memory amounts |
| connect(n) | connect current GPU to GPU n |
| disconnect(n) | disconnect from GPU n |
| canconnect(n) |
check whether connection is possible to GPU n |