From c026d3d8eb87a3637bd68ca7331e6615ddaa2ea3 Mon Sep 17 00:00:00 2001 From: mattdesl Date: Mon, 29 Jun 2015 17:59:27 -0400 Subject: [PATCH] bam! --- .gitignore | 5 ++ .npmignore | 10 +++ LICENSE.md | 21 +++++ README.md | 213 ++++++++++++++++++++++++++++++++++++++++++++ img/capital-pi1.png | Bin 0 -> 374 bytes img/hat.png | Bin 0 -> 140 bytes img/pipes1.png | Bin 0 -> 161 bytes img/pipes2.png | Bin 0 -> 159 bytes img/pipes3.png | Bin 0 -> 171 bytes img/pipes4.png | Bin 0 -> 182 bytes img/sigma1.png | Bin 0 -> 422 bytes img/sigma2.png | Bin 0 -> 660 bytes img/sigma3.png | Bin 0 -> 815 bytes package.json | 42 +++++++++ test.js | 19 ++++ 15 files changed, 310 insertions(+) create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 img/capital-pi1.png create mode 100644 img/hat.png create mode 100644 img/pipes1.png create mode 100644 img/pipes2.png create mode 100644 img/pipes3.png create mode 100644 img/pipes4.png create mode 100644 img/sigma1.png create mode 100644 img/sigma2.png create mode 100644 img/sigma3.png create mode 100644 package.json create mode 100644 test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9541c46 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +bower_components +node_modules +*.log +.DS_Store +bundle.js diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..13a18f9 --- /dev/null +++ b/.npmignore @@ -0,0 +1,10 @@ +bower_components +node_modules +*.log +.DS_Store +bundle.js +test +test.js +demo/ +.npmignore +LICENSE.md \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..9b10ce2 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) +Copyright (c) 2015 Jam3 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE +OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..dd40cae --- /dev/null +++ b/README.md @@ -0,0 +1,213 @@ +# math-as-code + +This can be used as a reference to ease developers into mathematical notation, showing some equations in code. + +Motivation: Academic papers can be intimidating for self-taught graphics programmers. :) + +If you see any errors or want to add/request more features, please [open a ticket](https://github.com/Jam3/math-as-code/issues) or send a PR. + +*Note:* For brevity, some code examples make use of [npm packages](https://www.npmjs.com/). You can always refer to their GitHub repos for implementation details. + +# contents + +- [sigma](#sigma) - *summation* +- [capital Pi](#capital-pi) - *products of sequences* +- [pipes](#pipes) + - [absolute](#absolute) + - [Euclidean norm](#euclidean-norm) + - [determinant](#determinant) +- [hat](#hat) - *unit vector* + +### sigma + +The big Greek "E" (Sigma) is for [Summation](https://en.wikipedia.org/wiki/Summation). In other words: summing up some numbers. + +![sigma](img/sigma1.png) + +Here, `i=1` says to start at `1` (lower bound) and end at `100` (upper bound). The `i` to the right of the "E" tells us what we are summing. In code: + +```js +var n = 100 +var sum = 0 +for (var i = 1; i <= n; i++) { + sum += i +} +``` + +The result of `sum` is `5050`. + +**Tip:** With whole numbers, this particular pattern can be optimized to the following: + +```js +var n = 100 +var sum = (n * (n + 1)) / 2 +``` + +Here is another example where the `i`, or the "what to sum", is different: + +![sum2](img/sigma2.png) + +In code: + +```js +var n = 4 +var sum = 0 +for (var i = 1; i <= n; i++) { + sum += (2 * i + 1) +} +``` + +The notation can be nested, which is much like nesting a `for` loop. You should evaluate the right-most sigma first, unless the author has enclosed them in parentheses to alter the order. + +![sigma3](img/sigma3.png) + +In code: + +```js +var sum = 0 +for (var i = 1; i <= 2; i++) { + for (var j = 4; j <= 6; j++) { + sum += (3 * i * j) + } +} +``` + +Here, `sum` will be `135`. + +### capital Pi + +The capital Pi or "Big Pi" is very similar to [Sigma](#sigma), except we are using multiplication to find the "products of sequences." + +Take the following: + +![capitalPi](img/capital-pi1.png) + +In code, it might look like this: + +```js +var n = 6 +var value = 1 +for (var i = 1; i <= n; i++) { + value *= i +} +``` + +Where `value` will evaluate to `720`. + +## pipes + +Pipes can mean different things depending on the context. Below are three common forms. + +#### absolute + +![pipes1](img/pipes1.png) + +For a number *x*, `| x |` means the absolute of *x*. In code: + +```js +var x = -5 +var result = Math.abs(x) +// => 5 +``` + +#### euclidean norm + +![pipes2](img/pipes2.png) + +For a vector *v*, `| v |` means the [Euclidean norm](https://en.wikipedia.org/wiki/Norm_%28mathematics%29#Euclidean_norm) of *v*. It is also referred to as the "magnitude" or "length" of a vector. + +Sometimes this is represented by double-pipes to avoid ambiguity with *absolute* notation. For example: + +![pipes4](img/pipes4.png) + +Here is an example using an `[x, y, z]` array to represent a 3D vector. + +```js +var v = [ 0, 4, -3 ] +length(v) +//=> 5 +``` + +The `length` function: + +```js +function length (a) { + var x = a[0], + y = a[1], + z = a[2] + return Math.sqrt(x*x + y*y + z*z) +} +``` + +Other implementations: + +- [magnitude](https://github.com/mattdesl/magnitude/blob/864ff5a7eb763d34bf154ac5f5332d7601192b70/index.js) - n-dimensional +- [gl-vec2/length](https://github.com/stackgl/gl-vec2/blob/21f460a371540258521fd2f720d80f14e87bd400/length.js) - 2D vector +- [gl-vec3/length](https://github.com/stackgl/gl-vec3/blob/507480fa57ba7c5fb70679cf531175a52c48cf53/length.js) - 3D vector + +#### determinant + +![pipes3](img/pipes3.png) + +For a matrix *A*, `| A |` means the [determinant](https://en.wikipedia.org/wiki/Determinant) of matrix *A*. + +Here is an example computing the determinant of a 2x2 matrix, represented by a flat array in column-major format. + +```js +var determinant = require('gl-mat4/determinant') + +var matrix = [1, 0, 0, 1] +var det = determinant(matrix) +//=> 1 +``` + +Implementations: + +- [gl-mat4/determinant](https://github.com/stackgl/gl-mat4/blob/c2e2de728fe7eba592f74cd02266100cc21ec89a/determinant.js) - also see [gl-mat3](https://github.com/stackgl/gl-mat3) and [gl-mat2](https://github.com/stackgl/gl-mat2) +- [ndarray-determinant](https://www.npmjs.com/package/ndarray-determinant) +- [glsl-determinant](https://www.npmjs.com/package/glsl-determinant) +- [robust-determinant](https://www.npmjs.com/package/robust-determinant) +- [robust-determinant-2](https://www.npmjs.com/package/robust-determinant-2) and [robust-determinant-3](https://www.npmjs.com/package/robust-determinant-3), specifically for 2x2 and 3x3 matrices, respectively + +#### hat + +The "hat" symbol above a character is used to represent a [unit vector](https://en.wikipedia.org/wiki/Unit_vector). For example, here is the unit vector of *a*: + +![hat](img/hat.png) + +In cartesian space, a unit vector is typically length 1, in the range of -1.0 to 1.0. Often we refer to this as *normalizing* a vector. + +```js +var a = [0, 4, -3] +normalize(a) +//=> [ 0, 0.8, -0.6000000000000001 ] +``` + +Notice, since we are working with floating point values and not using numerically robust operations, we do not get a perfect `-(3/5)` value for the Z component. + +Here is the `normalize` function, operating on 3D vectors: + +```js +function normalize(a) { + var x = a[0], + y = a[1], + z = a[2] + var len = x*x + y*y + z*z + if (len > 0) { + len = 1 / Math.sqrt(len) + a[0] = a[0] * len + a[1] = a[1] * len + a[2] = a[2] * len + } + return a +} +``` + +Other implementations: + +- [gl-vec3/normalize](https://github.com/stackgl/gl-vec3/blob/507480fa57ba7c5fb70679cf531175a52c48cf53/normalize.js) and [gl-vec2/normalize](https://github.com/stackgl/gl-vec2/blob/21f460a371540258521fd2f720d80f14e87bd400/normalize.js) +- [vectors/normalize-nd](https://github.com/hughsk/vectors/blob/master/normalize-nd.js) (n-dimensional) + +## License + +MIT, see [LICENSE.md](http://github.com/Jam3/math-as-code/blob/master/LICENSE.md) for details. diff --git a/img/capital-pi1.png b/img/capital-pi1.png new file mode 100644 index 0000000000000000000000000000000000000000..eb7c0127864e78667ffc0ab73a188d7df8ec9ba1 GIT binary patch literal 374 zcmV-+0g3)cNk%w1VITlD0OJ4v|Ns90004-Hh?tm|+}zxDc6PeDy6*1oA|fJ0L_`n} z5UQ%GGBPsE%*i#EA^8LW00000EC2ui03ZN1000F35Xecz5QOT@buoZYbR>Bo z(^0OPkqpDg&Y>d3G`ld8f##(K58wl?hBzas;SedG(5Q4ut#Sg`tai%rNXzi<7Z*mVGma2gZDOB?~qadme%&8HRssi8O~4eL0a6lN$yL2^y6W zmlOvB016EhodBN`AOr!NjdG8C4z7TR0eKXpw2rqO3n+A_sg@%GrmuIs983c$jJ1-n zddPvwkkq!gCBo7x+}13t0fih7SjH>{0|;3R1EmrK3l3TZ3(g%04Ey{Hvp48m%U2KeVT)Z=&{BPE=DUE zl+?W#HBS4yGCk^1tiYqob9kZ>$BC?EoK6`WK4Rymq+is~;c91E I77hk$03A>_lK=n! literal 0 HcmV?d00001 diff --git a/img/pipes2.png b/img/pipes2.png new file mode 100644 index 0000000000000000000000000000000000000000..9e2e7fcd15f4d85a9dc823132f506677015dcaa3 GIT binary patch literal 159 zcmZ?wbhEHb6krf$c+9}?|Nnmm28MU<-nqEAgoK2gIdf*!s#O940(b7*nK5IAk&)4^ zUAsCuI?Bq*l$4ax($W-vvM>TQFzA2?kQodtJ^>A$*`kNDtUg`4%U-4AKFz^Gv`t;6 z!;+J;KxFc!69@WOMBH)~%`)MbB9;HhKtTQFzA2?kQodt5djUJtLG-&iEV!toLC~C&ce>gamzB!l}1TMDZC1@`^V@0&pNq6pJ6}mP7+%zmhWvS{YU~AOrBw)Z9 kwYPB@z=+wZg-0R_URGf;5)FN4Nk%w1VIu%G0OJ4v|Ns90007+F+(bk~n3$M$c6K5nBD%V|W@cuHh=|P0 z%rY`ER8&+D5D==Ws_yRYA^8LW00000EC2ui03!f4000F35J)M;REZ>s_55H65QQ-< z5sSZcYicN}ynfH``~QG^4FLfShKGoTgo}g@QUruwNCE^5ge^z|gd1cJ z1px|D4}+k24oy-Dgfwgk4_gU@L4hF)je!nFAEShEe-FNGm$V{p0jR_uwgCrx2M&^D zgblBG4=W9!Fbn^PmX3P`$S@0>d)Yh;vw99D%_0Su<#_5J@zs0^2n75tGzgYV z2s<_p0>*e?fE=51JQm=|Q)!F4JOV7u<55o`2w+qS9MoGSg&U)jC{Ir0k)VLfmoQ_> z^jNT_&6_wyN~yqyCI}L&Y?^8HE=kZWD-0w=;I64VUqi_VuxGT~i%CbfCs_55Hcj$~Q1$&5zii?bmj*pO$l9N*n0Rat}nwy!I zo|g`kMFf{@CISQumolP51D6{%4+Q}VtwRr%wKxt~w>S!yJ2wdrAP+FV$sh@rN>T{M zzYo433!qX72Ok;;Kp+lKXAc9m9}deCxtD-NtTWOA0io*cBntK;0)heC)*=U16+!pG zfH?pRhS?zC??k^4c|!OJQ0>Nmd<~Y-8p6=VLlFK7Oj*0s(1y4X2?;q6Iih2Yk04Hh zkcUZ{NfHm{0YC9zyJRL217W-dEBuiUAB1rVgAl?{ u6*Xv))y{(uQsA2i9Cj#(Run>5RcaY(NI{7W7LdSgA@uP_hctE(0RTHr91d0h literal 0 HcmV?d00001 diff --git a/img/sigma3.png b/img/sigma3.png new file mode 100644 index 0000000000000000000000000000000000000000..7744d7bdbbc3689647c8ce98b0583d25a643c807 GIT binary patch literal 815 zcmV+~1JL|ONk%w1VOsz=0OJ4v|Ns90006qWx`>E~c6N4VW@hg0?wFXEA|fI(GBVuU z+*DLl%*@QHs;UqW5JW^oA^8LW00000EC2ui09yb!000F35XeczAcgA9^*Dpljbv$R z#vv8NX%Yf(xpYkmh;$@2XXFw?Z#WbI2n%B6aX7S;&>F%B9XNE)sCKl7X&XEMZcE+_ z7yzo05gwP{jfO!n*oK|&vLe6_WT6ZNGkJuChKGoWii?bmj*pNO4tx%kmY0=%nhhM2 z0hpkkngN`P2Ye4F3I_{(VH&3asV1wf0k4V=e044c1OaFpx&gc=zQBwIp202+85_!c z%qPx`417Q|140|u0og9w6e(LOW_(Fo1L#l%+))P48VP)*TJ)qu1712H3+59VTp`f_ z;Dl%a)F?ry&;>36<_rWdIHy|x3b-Z+a7AY0tsedbDHNCmz(Kka(>`!0z(-ybmH}Lz zc+fDySt&f~)`3cj8u267-kQa}TpVw}B`AXLMiPfL|Nbr6+A(|`pQa3kk{nE(<0 zG+gY^02P_CvRo(tr-4CNF=@t1D22eD1O!zlw6YszuUfVm76`=vEkIPfBN7y|v$%w^ zz|4Lbd8@&VBrYenEXaJK=^lHn=H>uOSi{u`1TscR-Ge3t0$UyMBG9670=_vpG(dco zca0_<2n<+YFVKOd5~B{_3&3qnBF={xKw87{1l0;C_sT0F`!o{<-VyjtgGFV70Ddqp zA#+?^8#3FIP{trT^7aY-oM4q$zX|^1px!qh69p)ATwt7)pg=1?>6IV}3kJ}jA*pc} zgaQmSkQz1}R5%}o8sY<(A!vg&kiem+YViO8egYW*06XSrLl*!5 literal 0 HcmV?d00001 diff --git a/package.json b/package.json new file mode 100644 index 0000000..beb9e93 --- /dev/null +++ b/package.json @@ -0,0 +1,42 @@ +{ + "name": "math-as-code", + "version": "1.0.0", + "description": "a reference for mathematical notation in code form", + "main": "index.js", + "license": "MIT", + "author": { + "name": "Matt DesLauriers", + "email": "dave.des@gmail.com", + "url": "https://github.com/mattdesl" + }, + "dependencies": {}, + "devDependencies": {}, + "scripts": { + "test": "node test.js" + }, + "keywords": [ + "learning", + "math", + "mathematical", + "notation", + "symbol", + "symbols", + "cheat", + "sheet", + "cheatsheet", + "help", + "guide", + "walkthrough", + "tutorial", + "mathematics", + "language" + ], + "repository": { + "type": "git", + "url": "git://github.com/Jam3/math-as-code.git" + }, + "homepage": "https://github.com/Jam3/math-as-code", + "bugs": { + "url": "https://github.com/Jam3/math-as-code/issues" + } +} \ No newline at end of file diff --git a/test.js b/test.js new file mode 100644 index 0000000..fce17d2 --- /dev/null +++ b/test.js @@ -0,0 +1,19 @@ +var a = [0, 4, -3] +var normalizedA = normalize(a) + +//=> [ 0, 0.8, -0.6000000000000001 ] + +function normalize(a) { + var x = a[0], + y = a[1], + z = a[2] + var len = x*x + y*y + z*z + if (len > 0) { + len = 1 / Math.sqrt(len) + a[0] = a[0] * len + a[1] = a[1] * len + a[2] = a[2] * len + } + return a +} +console.log(normalizedA) \ No newline at end of file