|
| 1 | +ndarray-ops |
| 2 | +=========== |
| 3 | +A collection of common mathematical operations for [ndarrays](http://github.com/mikolalysenko/ndarrays). Implemented using [cwise](http://github.com/mikolalysenko/cwise) |
| 4 | + |
| 5 | +Usage |
| 6 | +===== |
| 7 | +First, install the library using npm: |
| 8 | + |
| 9 | + npm install ndarray-ops |
| 10 | + |
| 11 | +Then you can import the library by doing: |
| 12 | + |
| 13 | + var ops = require("ndarray-ops") |
| 14 | + |
| 15 | +Then you can use the functions as in the following example: |
| 16 | + |
| 17 | +```javascript |
| 18 | +//First, import libraries |
| 19 | +var ndarray = require("ndarray") |
| 20 | + , ops = require("ndarray-ops") |
| 21 | + |
| 22 | + |
| 23 | +//Next, create some arrays |
| 24 | +var a = ndarray.zeros([128,128]) |
| 25 | + , b = ndarray.zeros([128,128]) |
| 26 | + , c = ndarray.zeros([128,128]) |
| 27 | + |
| 28 | +//Initialize b with some random numbers: |
| 29 | +ops.random(b) |
| 30 | + |
| 31 | +//Set c to a constant 1 |
| 32 | +ops.assigns(c, 1.0) |
| 33 | + |
| 34 | +//Add b and c, store result in a: |
| 35 | +ops.add(a, b, c) |
| 36 | + |
| 37 | +//Multiply a by 0.5 in place |
| 38 | +ops.mulseq(a, 0.5) |
| 39 | + |
| 40 | +//Print some statistics about a: |
| 41 | +console.log( |
| 42 | + "inf(a) = ", ops.inf(a), |
| 43 | + "sup(a) = ", ops.sup(a), |
| 44 | + "argmin(a) = ", ops.argmin(a), |
| 45 | + "argmax(a) = ", ops.argmax(a), |
| 46 | + "norm1(a) = ", ops.norm1(a)) |
| 47 | +``` |
| 48 | + |
| 49 | +Conventions |
| 50 | +=========== |
| 51 | +This library implements component-wise operations for all of the operators and Math.* functions in JS, along with a few commonly used aggregate operations. Most of the functions in the library work by applying some commutative binary operator to a pair of arrays. You call them like this: |
| 52 | + |
| 53 | +```javascript |
| 54 | +ops.add(dest, arg1, arg2) |
| 55 | +``` |
| 56 | + |
| 57 | +Which translates into code that works (approximately) like this: |
| 58 | + |
| 59 | +```javascript |
| 60 | +for(var i=0; i<dest.shape[0]; ++i) { |
| 61 | + dest[i] = arg1[i] + arg2[i] |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +It is up to you to specify where the result gets store. This library does not create new arrays for you to avoid performing expensive intermediate allocations. There are also a few other variations: |
| 66 | + |
| 67 | +```javascript |
| 68 | +ops.addeq(dest, arg1) |
| 69 | +``` |
| 70 | +Operators with the -eq suffix perform an assignment. |
| 71 | + |
| 72 | +```javascript |
| 73 | +for(var i=0; i<dest.shape[0]; ++i) { |
| 74 | + dest[i] += arg1[i] |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +```javascript |
| 79 | +ops.adds(dest, arg1, 1.0) |
| 80 | +``` |
| 81 | +The -s suffix denotes scalar/broadcast operations; so the above would translate to: |
| 82 | + |
| 83 | +```javascript |
| 84 | +for(var i=0; i<dest.shape[0]; ++i) { |
| 85 | + dest[i] = arg1[i] + 1.0 |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +```javascript |
| 90 | +ops.addseq(dest, 1.0) |
| 91 | +``` |
| 92 | +The -seq suffix is basically the combination of the above, and translates to: |
| 93 | + |
| 94 | +```javascript |
| 95 | +for(var i=0; i<dest.shape[0]; ++i) { |
| 96 | + dest[i] += 1.0 |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +The following operators follow this rule: |
| 101 | + |
| 102 | +* add |
| 103 | +* addeq |
| 104 | +* adds |
| 105 | +* addseq |
| 106 | +* sub |
| 107 | +* subeq |
| 108 | +* subs |
| 109 | +* subseq |
| 110 | +* mul |
| 111 | +* muleq |
| 112 | +* muls |
| 113 | +* mulseq |
| 114 | +* div |
| 115 | +* diveq |
| 116 | +* divs |
| 117 | +* divseq |
| 118 | +* mod |
| 119 | +* modeq |
| 120 | +* mods |
| 121 | +* modseq |
| 122 | +* band |
| 123 | +* bandeq |
| 124 | +* bands |
| 125 | +* bandseq |
| 126 | +* bor |
| 127 | +* boreq |
| 128 | +* bors |
| 129 | +* borseq |
| 130 | +* bxor |
| 131 | +* bxoreq |
| 132 | +* bxors |
| 133 | +* bxorseq |
| 134 | +* lshift |
| 135 | +* lshifteq |
| 136 | +* lshifts |
| 137 | +* lshiftseq |
| 138 | +* rshift |
| 139 | +* rshifteq |
| 140 | +* rshifts |
| 141 | +* rshiftseq |
| 142 | +* rrshift |
| 143 | +* rrshifteq |
| 144 | +* rrshifts |
| 145 | +* rrshiftseq |
| 146 | +* lt |
| 147 | +* lts |
| 148 | +* lteq |
| 149 | +* ltseq |
| 150 | +* gt |
| 151 | +* gts |
| 152 | +* gteq |
| 153 | +* gtseq |
| 154 | +* leq |
| 155 | +* leqs |
| 156 | +* leqeq |
| 157 | +* leqseq |
| 158 | +* geq |
| 159 | +* geqs |
| 160 | +* geqeq |
| 161 | +* geqseq |
| 162 | +* max |
| 163 | +* maxs |
| 164 | +* maxeq |
| 165 | +* maxseq |
| 166 | +* min |
| 167 | +* mins |
| 168 | +* mineq |
| 169 | +* minseq |
| 170 | +* and |
| 171 | +* ands |
| 172 | +* andeq |
| 173 | +* andseq |
| 174 | +* or |
| 175 | +* ors |
| 176 | +* oreq |
| 177 | +* orseq |
| 178 | +* eq |
| 179 | +* eqs |
| 180 | +* eqeq |
| 181 | +* eqseq |
| 182 | +* neq |
| 183 | +* neqs |
| 184 | +* neqeq |
| 185 | +* neqseq |
| 186 | + |
| 187 | + |
| 188 | +Special Cases |
| 189 | +------------- |
| 190 | +There are a few corner cases that follow slightly different rules. These can be grouped using the following general categories: |
| 191 | + |
| 192 | +### Assignment |
| 193 | + |
| 194 | +* assign |
| 195 | +* assigns |
| 196 | + |
| 197 | +### Nullary operators |
| 198 | + |
| 199 | +* random |
| 200 | + |
| 201 | +### Unary operators |
| 202 | + |
| 203 | +* not |
| 204 | +* noteq |
| 205 | +* bnot |
| 206 | +* bnoteq |
| 207 | +* neg |
| 208 | +* negeq |
| 209 | +* abs |
| 210 | +* abseq |
| 211 | +* acos |
| 212 | +* acoseq |
| 213 | +* asin |
| 214 | +* asineq |
| 215 | +* atan |
| 216 | +* ataneq |
| 217 | +* ceil |
| 218 | +* ceileq |
| 219 | +* cos |
| 220 | +* coseq |
| 221 | +* exp |
| 222 | +* expeq |
| 223 | +* floor |
| 224 | +* flooreq |
| 225 | +* log |
| 226 | +* logeq |
| 227 | +* round |
| 228 | +* roundeq |
| 229 | +* sin |
| 230 | +* sineq |
| 231 | +* sqrt |
| 232 | +* sqrteq |
| 233 | +* tan |
| 234 | +* taneq |
| 235 | + |
| 236 | + |
| 237 | +### Non-symmetric binary operators |
| 238 | + |
| 239 | +* atan2 |
| 240 | +* atan2s |
| 241 | +* atan2sop |
| 242 | +* atan2eq |
| 243 | +* atan2seq |
| 244 | +* atan2opeq |
| 245 | +* atan2sopeq |
| 246 | +* pow |
| 247 | +* pows |
| 248 | +* powsop |
| 249 | +* poweq |
| 250 | +* powseq |
| 251 | +* powopeq |
| 252 | +* powsopeq |
| 253 | + |
| 254 | +### Map-reduce (aggregate) operators |
| 255 | + |
| 256 | +* any |
| 257 | +* all |
| 258 | +* sum |
| 259 | +* prod |
| 260 | +* norm2squared |
| 261 | +* norm2 |
| 262 | +* norminf |
| 263 | +* norm1 |
| 264 | +* sup |
| 265 | +* inf |
| 266 | +* argmin |
| 267 | +* argmax |
| 268 | + |
| 269 | + |
| 270 | +Credits |
| 271 | +======= |
| 272 | +(c) 2013 Mikola Lysenko. BSD |
0 commit comments