| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- "use strict"
- var compile = require("cwise-compiler")
- var EmptyProc = {
- body: "",
- args: [],
- thisVars: [],
- localVars: []
- }
- function fixup(x) {
- if(!x) {
- return EmptyProc
- }
- for(var i=0; i<x.args.length; ++i) {
- var a = x.args[i]
- if(i === 0) {
- x.args[i] = {name: a, lvalue:true, rvalue: !!x.rvalue, count:x.count||1 }
- } else {
- x.args[i] = {name: a, lvalue:false, rvalue:true, count: 1}
- }
- }
- if(!x.thisVars) {
- x.thisVars = []
- }
- if(!x.localVars) {
- x.localVars = []
- }
- return x
- }
- function pcompile(user_args) {
- return compile({
- args: user_args.args,
- pre: fixup(user_args.pre),
- body: fixup(user_args.body),
- post: fixup(user_args.proc),
- funcName: user_args.funcName
- })
- }
- function makeOp(user_args) {
- var args = []
- for(var i=0; i<user_args.args.length; ++i) {
- args.push("a"+i)
- }
- var wrapper = new Function("P", [
- "return function ", user_args.funcName, "_ndarrayops(", args.join(","), ") {P(", args.join(","), ");return a0}"
- ].join(""))
- return wrapper(pcompile(user_args))
- }
- var assign_ops = {
- add: "+",
- sub: "-",
- mul: "*",
- div: "/",
- mod: "%",
- band: "&",
- bor: "|",
- bxor: "^",
- lshift: "<<",
- rshift: ">>",
- rrshift: ">>>"
- }
- ;(function(){
- for(var id in assign_ops) {
- var op = assign_ops[id]
- exports[id] = makeOp({
- args: ["array","array","array"],
- body: {args:["a","b","c"],
- body: "a=b"+op+"c"},
- funcName: id
- })
- exports[id+"eq"] = makeOp({
- args: ["array","array"],
- body: {args:["a","b"],
- body:"a"+op+"=b"},
- rvalue: true,
- funcName: id+"eq"
- })
- exports[id+"s"] = makeOp({
- args: ["array", "array", "scalar"],
- body: {args:["a","b","s"],
- body:"a=b"+op+"s"},
- funcName: id+"s"
- })
- exports[id+"seq"] = makeOp({
- args: ["array","scalar"],
- body: {args:["a","s"],
- body:"a"+op+"=s"},
- rvalue: true,
- funcName: id+"seq"
- })
- }
- })();
- var unary_ops = {
- not: "!",
- bnot: "~",
- neg: "-",
- recip: "1.0/"
- }
- ;(function(){
- for(var id in unary_ops) {
- var op = unary_ops[id]
- exports[id] = makeOp({
- args: ["array", "array"],
- body: {args:["a","b"],
- body:"a="+op+"b"},
- funcName: id
- })
- exports[id+"eq"] = makeOp({
- args: ["array"],
- body: {args:["a"],
- body:"a="+op+"a"},
- rvalue: true,
- count: 2,
- funcName: id+"eq"
- })
- }
- })();
- var binary_ops = {
- and: "&&",
- or: "||",
- eq: "===",
- neq: "!==",
- lt: "<",
- gt: ">",
- leq: "<=",
- geq: ">="
- }
- ;(function() {
- for(var id in binary_ops) {
- var op = binary_ops[id]
- exports[id] = makeOp({
- args: ["array","array","array"],
- body: {args:["a", "b", "c"],
- body:"a=b"+op+"c"},
- funcName: id
- })
- exports[id+"s"] = makeOp({
- args: ["array","array","scalar"],
- body: {args:["a", "b", "s"],
- body:"a=b"+op+"s"},
- funcName: id+"s"
- })
- exports[id+"eq"] = makeOp({
- args: ["array", "array"],
- body: {args:["a", "b"],
- body:"a=a"+op+"b"},
- rvalue:true,
- count:2,
- funcName: id+"eq"
- })
- exports[id+"seq"] = makeOp({
- args: ["array", "scalar"],
- body: {args:["a","s"],
- body:"a=a"+op+"s"},
- rvalue:true,
- count:2,
- funcName: id+"seq"
- })
- }
- })();
- var math_unary = [
- "abs",
- "acos",
- "asin",
- "atan",
- "ceil",
- "cos",
- "exp",
- "floor",
- "log",
- "round",
- "sin",
- "sqrt",
- "tan"
- ]
- ;(function() {
- for(var i=0; i<math_unary.length; ++i) {
- var f = math_unary[i]
- exports[f] = makeOp({
- args: ["array", "array"],
- pre: {args:[], body:"this_f=Math."+f, thisVars:["this_f"]},
- body: {args:["a","b"], body:"a=this_f(b)", thisVars:["this_f"]},
- funcName: f
- })
- exports[f+"eq"] = makeOp({
- args: ["array"],
- pre: {args:[], body:"this_f=Math."+f, thisVars:["this_f"]},
- body: {args: ["a"], body:"a=this_f(a)", thisVars:["this_f"]},
- rvalue: true,
- count: 2,
- funcName: f+"eq"
- })
- }
- })();
- var math_comm = [
- "max",
- "min",
- "atan2",
- "pow"
- ]
- ;(function(){
- for(var i=0; i<math_comm.length; ++i) {
- var f= math_comm[i]
- exports[f] = makeOp({
- args:["array", "array", "array"],
- pre: {args:[], body:"this_f=Math."+f, thisVars:["this_f"]},
- body: {args:["a","b","c"], body:"a=this_f(b,c)", thisVars:["this_f"]},
- funcName: f
- })
- exports[f+"s"] = makeOp({
- args:["array", "array", "scalar"],
- pre: {args:[], body:"this_f=Math."+f, thisVars:["this_f"]},
- body: {args:["a","b","c"], body:"a=this_f(b,c)", thisVars:["this_f"]},
- funcName: f+"s"
- })
- exports[f+"eq"] = makeOp({ args:["array", "array"],
- pre: {args:[], body:"this_f=Math."+f, thisVars:["this_f"]},
- body: {args:["a","b"], body:"a=this_f(a,b)", thisVars:["this_f"]},
- rvalue: true,
- count: 2,
- funcName: f+"eq"
- })
- exports[f+"seq"] = makeOp({ args:["array", "scalar"],
- pre: {args:[], body:"this_f=Math."+f, thisVars:["this_f"]},
- body: {args:["a","b"], body:"a=this_f(a,b)", thisVars:["this_f"]},
- rvalue:true,
- count:2,
- funcName: f+"seq"
- })
- }
- })();
- var math_noncomm = [
- "atan2",
- "pow"
- ]
- ;(function(){
- for(var i=0; i<math_noncomm.length; ++i) {
- var f= math_noncomm[i]
- exports[f+"op"] = makeOp({
- args:["array", "array", "array"],
- pre: {args:[], body:"this_f=Math."+f, thisVars:["this_f"]},
- body: {args:["a","b","c"], body:"a=this_f(c,b)", thisVars:["this_f"]},
- funcName: f+"op"
- })
- exports[f+"ops"] = makeOp({
- args:["array", "array", "scalar"],
- pre: {args:[], body:"this_f=Math."+f, thisVars:["this_f"]},
- body: {args:["a","b","c"], body:"a=this_f(c,b)", thisVars:["this_f"]},
- funcName: f+"ops"
- })
- exports[f+"opeq"] = makeOp({ args:["array", "array"],
- pre: {args:[], body:"this_f=Math."+f, thisVars:["this_f"]},
- body: {args:["a","b"], body:"a=this_f(b,a)", thisVars:["this_f"]},
- rvalue: true,
- count: 2,
- funcName: f+"opeq"
- })
- exports[f+"opseq"] = makeOp({ args:["array", "scalar"],
- pre: {args:[], body:"this_f=Math."+f, thisVars:["this_f"]},
- body: {args:["a","b"], body:"a=this_f(b,a)", thisVars:["this_f"]},
- rvalue:true,
- count:2,
- funcName: f+"opseq"
- })
- }
- })();
- exports.any = compile({
- args:["array"],
- pre: EmptyProc,
- body: {args:[{name:"a", lvalue:false, rvalue:true, count:1}], body: "if(a){return true}", localVars: [], thisVars: []},
- post: {args:[], localVars:[], thisVars:[], body:"return false"},
- funcName: "any"
- })
- exports.all = compile({
- args:["array"],
- pre: EmptyProc,
- body: {args:[{name:"x", lvalue:false, rvalue:true, count:1}], body: "if(!x){return false}", localVars: [], thisVars: []},
- post: {args:[], localVars:[], thisVars:[], body:"return true"},
- funcName: "all"
- })
- exports.sum = compile({
- args:["array"],
- pre: {args:[], localVars:[], thisVars:["this_s"], body:"this_s=0"},
- body: {args:[{name:"a", lvalue:false, rvalue:true, count:1}], body: "this_s+=a", localVars: [], thisVars: ["this_s"]},
- post: {args:[], localVars:[], thisVars:["this_s"], body:"return this_s"},
- funcName: "sum"
- })
- exports.prod = compile({
- args:["array"],
- pre: {args:[], localVars:[], thisVars:["this_s"], body:"this_s=1"},
- body: {args:[{name:"a", lvalue:false, rvalue:true, count:1}], body: "this_s*=a", localVars: [], thisVars: ["this_s"]},
- post: {args:[], localVars:[], thisVars:["this_s"], body:"return this_s"},
- funcName: "prod"
- })
- exports.norm2squared = compile({
- args:["array"],
- pre: {args:[], localVars:[], thisVars:["this_s"], body:"this_s=0"},
- body: {args:[{name:"a", lvalue:false, rvalue:true, count:2}], body: "this_s+=a*a", localVars: [], thisVars: ["this_s"]},
- post: {args:[], localVars:[], thisVars:["this_s"], body:"return this_s"},
- funcName: "norm2squared"
- })
-
- exports.norm2 = compile({
- args:["array"],
- pre: {args:[], localVars:[], thisVars:["this_s"], body:"this_s=0"},
- body: {args:[{name:"a", lvalue:false, rvalue:true, count:2}], body: "this_s+=a*a", localVars: [], thisVars: ["this_s"]},
- post: {args:[], localVars:[], thisVars:["this_s"], body:"return Math.sqrt(this_s)"},
- funcName: "norm2"
- })
-
- exports.norminf = compile({
- args:["array"],
- pre: {args:[], localVars:[], thisVars:["this_s"], body:"this_s=0"},
- body: {args:[{name:"a", lvalue:false, rvalue:true, count:4}], body:"if(-a>this_s){this_s=-a}else if(a>this_s){this_s=a}", localVars: [], thisVars: ["this_s"]},
- post: {args:[], localVars:[], thisVars:["this_s"], body:"return this_s"},
- funcName: "norminf"
- })
- exports.norm1 = compile({
- args:["array"],
- pre: {args:[], localVars:[], thisVars:["this_s"], body:"this_s=0"},
- body: {args:[{name:"a", lvalue:false, rvalue:true, count:3}], body: "this_s+=a<0?-a:a", localVars: [], thisVars: ["this_s"]},
- post: {args:[], localVars:[], thisVars:["this_s"], body:"return this_s"},
- funcName: "norm1"
- })
- exports.sup = compile({
- args: [ "array" ],
- pre:
- { body: "this_h=-Infinity",
- args: [],
- thisVars: [ "this_h" ],
- localVars: [] },
- body:
- { body: "if(_inline_1_arg0_>this_h)this_h=_inline_1_arg0_",
- args: [{"name":"_inline_1_arg0_","lvalue":false,"rvalue":true,"count":2} ],
- thisVars: [ "this_h" ],
- localVars: [] },
- post:
- { body: "return this_h",
- args: [],
- thisVars: [ "this_h" ],
- localVars: [] }
- })
- exports.inf = compile({
- args: [ "array" ],
- pre:
- { body: "this_h=Infinity",
- args: [],
- thisVars: [ "this_h" ],
- localVars: [] },
- body:
- { body: "if(_inline_1_arg0_<this_h)this_h=_inline_1_arg0_",
- args: [{"name":"_inline_1_arg0_","lvalue":false,"rvalue":true,"count":2} ],
- thisVars: [ "this_h" ],
- localVars: [] },
- post:
- { body: "return this_h",
- args: [],
- thisVars: [ "this_h" ],
- localVars: [] }
- })
- exports.argmin = compile({
- args:["index","array","shape"],
- pre:{
- body:"{this_v=Infinity;this_i=_inline_0_arg2_.slice(0)}",
- args:[
- {name:"_inline_0_arg0_",lvalue:false,rvalue:false,count:0},
- {name:"_inline_0_arg1_",lvalue:false,rvalue:false,count:0},
- {name:"_inline_0_arg2_",lvalue:false,rvalue:true,count:1}
- ],
- thisVars:["this_i","this_v"],
- localVars:[]},
- body:{
- body:"{if(_inline_1_arg1_<this_v){this_v=_inline_1_arg1_;for(var _inline_1_k=0;_inline_1_k<_inline_1_arg0_.length;++_inline_1_k){this_i[_inline_1_k]=_inline_1_arg0_[_inline_1_k]}}}",
- args:[
- {name:"_inline_1_arg0_",lvalue:false,rvalue:true,count:2},
- {name:"_inline_1_arg1_",lvalue:false,rvalue:true,count:2}],
- thisVars:["this_i","this_v"],
- localVars:["_inline_1_k"]},
- post:{
- body:"{return this_i}",
- args:[],
- thisVars:["this_i"],
- localVars:[]}
- })
- exports.argmax = compile({
- args:["index","array","shape"],
- pre:{
- body:"{this_v=-Infinity;this_i=_inline_0_arg2_.slice(0)}",
- args:[
- {name:"_inline_0_arg0_",lvalue:false,rvalue:false,count:0},
- {name:"_inline_0_arg1_",lvalue:false,rvalue:false,count:0},
- {name:"_inline_0_arg2_",lvalue:false,rvalue:true,count:1}
- ],
- thisVars:["this_i","this_v"],
- localVars:[]},
- body:{
- body:"{if(_inline_1_arg1_>this_v){this_v=_inline_1_arg1_;for(var _inline_1_k=0;_inline_1_k<_inline_1_arg0_.length;++_inline_1_k){this_i[_inline_1_k]=_inline_1_arg0_[_inline_1_k]}}}",
- args:[
- {name:"_inline_1_arg0_",lvalue:false,rvalue:true,count:2},
- {name:"_inline_1_arg1_",lvalue:false,rvalue:true,count:2}],
- thisVars:["this_i","this_v"],
- localVars:["_inline_1_k"]},
- post:{
- body:"{return this_i}",
- args:[],
- thisVars:["this_i"],
- localVars:[]}
- })
- exports.random = makeOp({
- args: ["array"],
- pre: {args:[], body:"this_f=Math.random", thisVars:["this_f"]},
- body: {args: ["a"], body:"a=this_f()", thisVars:["this_f"]},
- funcName: "random"
- })
- exports.assign = makeOp({
- args:["array", "array"],
- body: {args:["a", "b"], body:"a=b"},
- funcName: "assign" })
- exports.assigns = makeOp({
- args:["array", "scalar"],
- body: {args:["a", "b"], body:"a=b"},
- funcName: "assigns" })
- exports.equals = compile({
- args:["array", "array"],
- pre: EmptyProc,
- body: {args:[{name:"x", lvalue:false, rvalue:true, count:1},
- {name:"y", lvalue:false, rvalue:true, count:1}],
- body: "if(x!==y){return false}",
- localVars: [],
- thisVars: []},
- post: {args:[], localVars:[], thisVars:[], body:"return true"},
- funcName: "equals"
- })
|