Skip to content

Commit 4e0cc53

Browse files
Added optimization to compiler and bumped Copyright
1 parent 98fb7f6 commit 4e0cc53

File tree

6 files changed

+65
-15
lines changed

6 files changed

+65
-15
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2015 Patrick Roberts
1+
Copyright (c) 2016 Patrick Roberts
22

33
Permission is hereby granted, free of charge, to any person
44
obtaining a copy of this software and associated documentation

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ input.addEventListener('change', function () {
180180

181181
Note that the compiler creates a function rather than evaluating the
182182
expression that is compiled immediately. The function returned is
183-
high-performace, since it caches all real-values in the expression
183+
high-performace, since it caches all constant expressions in the string
184184
so that they don't need to be re-evaluated with each call.
185185

186186
The following is an example where the compiler provides parameters
@@ -1076,3 +1076,27 @@ __Arguments__
10761076
* `string` - A human-readable `String` of a math expression to be compiled.
10771077
* `params` - An optional `Array[String]` of human-readable parameters to parse.
10781078
* `skipFormat` - An optional `Boolean` to determine whether to skip pre-formatting.
1079+
1080+
## License
1081+
1082+
Copyright (c) 2016 Patrick Roberts
1083+
1084+
Permission is hereby granted, free of charge, to any person
1085+
obtaining a copy of this software and associated documentation
1086+
files (the "Software"), to deal in the Software without restriction,
1087+
including without limitation the rights to use, copy, modify,
1088+
merge, publish, distribute, sublicense, and/or sell copies of the
1089+
Software, and to permit persons to whom the Software is furnished
1090+
to do so, subject to the following conditions:
1091+
1092+
The above copyright notice and this permission notice shall be
1093+
included in all copies or substantial portions of the Software.
1094+
1095+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1096+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
1097+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1098+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
1099+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
1100+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1101+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1102+
OTHER DEALINGS IN THE SOFTWARE.

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "complex-js",
33
"description": "Complex math for the browser and Node.js",
4-
"version": "3.1.3",
4+
"version": "3.2.0",
55
"authors": [
66
"Patrick Roberts"
77
],

lib/complex.js

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2015 Patrick Roberts
2+
* Copyright (c) 2016 Patrick Roberts
33
*
44
* Permission is hereby granted, free of charge, to any person
55
* obtaining a copy of this software and associated documentation
@@ -180,13 +180,13 @@
180180

181181
if(Math.sinh === undefined) {
182182
Math.sinh = function(x) {
183-
return (-Math.exp(-x) + Math.exp(x)) / 2;
183+
return (Math.exp(x) - Math.exp(-x)) / 2;
184184
};
185185
}
186186

187187
if(Math.cosh === undefined) {
188188
Math.cosh = function(x) {
189-
return (Math.exp(-x) + Math.exp(x)) / 2;
189+
return (Math.exp(x) + Math.exp(-x)) / 2;
190190
};
191191
}
192192

@@ -210,6 +210,8 @@
210210
*/
211211

212212
function Complex(r, i, m, t) {
213+
i = i || 0;
214+
213215
if(!(this instanceof Complex)) {
214216
return new Complex(r, i, m, t);
215217
}
@@ -1254,7 +1256,7 @@
12541256
throw new SyntaxError('"' + arg + '" is an invalid variable name.');
12551257
}
12561258

1257-
return name.toLowerCase();
1259+
return name;
12581260
});
12591261

12601262
for(i = 0; i < args.length; i++) {
@@ -1454,7 +1456,19 @@
14541456
// ignores extraneous grouping
14551457
str += (func === '(' ? '' : func);
14561458
i += match.length;
1457-
str += compile(exp.substring(i, j), vars, namespace) + (func === '(' ? '' : ')');
1459+
// efficiently compiles subgroup by first assuming a constant expression
1460+
try {
1461+
var ns = new Namespace();
1462+
var body = compile(exp.substring(i, j), [], ns, true);
1463+
var comp = (new Function('', 'var C=this.Complex;return ' + body + ';')).bind(ns)();
1464+
str += 'this[' + namespace.push(comp) + ']';
1465+
// then compiling with specified variables if that fails
1466+
} catch(e) {
1467+
str += compile(exp.substring(i, j), vars, namespace, true);
1468+
} finally {
1469+
str += (func === '(' ? '' : ')');
1470+
}
1471+
14581472
i = (exp[j] === ')' ? j + 1 : j);
14591473
findOper = true;
14601474
}
@@ -1495,7 +1509,19 @@
14951509

14961510
str += oper;
14971511
i += match.length;
1498-
str += compile(exp.substring(i, j), vars, namespace) + ')';
1512+
// efficiently compiles subgroup by first assuming a constant expression
1513+
try {
1514+
var ns = new Namespace();
1515+
var body = compile(exp.substring(i, j), [], ns, true);
1516+
var comp = (new Function('', 'var C=this.Complex;return ' + body + ';')).bind(ns)();
1517+
str += 'this[' + namespace.push(comp) + ']';
1518+
// then compiling with specified variables if that fails
1519+
} catch(e) {
1520+
str += compile(exp.substring(i, j), vars, namespace, true);
1521+
} finally {
1522+
str += ')';
1523+
}
1524+
14991525
i = (exp[j] === ')' ? j + 1 : j);
15001526
findOper = true;
15011527
// no identifiable expressions left to parse
@@ -1522,11 +1548,10 @@
15221548
if(typeof args === 'boolean') {
15231549
skipFormat = args;
15241550
args = [];
1525-
} else if(!(typeof args === 'object' && args !== null && args.constructor === Array)) {
1551+
} else if(!Array.isArray(args)) {
15261552
args = [];
15271553
}
1528-
// make everything lowercase because it's easier
1529-
str = str.toLowerCase();
1554+
15301555
// default is to format the human-readable string
15311556
if(!skipFormat) {
15321557
str = Complex.formatFunction(str);
@@ -1538,11 +1563,12 @@
15381563
// create namespace to bind to function
15391564
namespace = new Namespace(),
15401565
// compile function body from string given valid arguments and namespace to cache parsed numerical constants
1541-
body = compile(str, vars, namespace),
1566+
body = compile('(' + str + ')', vars, namespace),
15421567
// convert compiled variable names map to array for function arguments
15431568
params = args.map(function(arg) {
15441569
return vars[arg];
15451570
}).join(',');
1571+
15461572
// the constructed function being returned
15471573
return (new Function(params, 'var C=this.Complex;return ' + body + ';')).bind(namespace);
15481574
};

lib/complex.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
],
88
"author": "Patrick Roberts",
99
"main": "./index",
10-
"version": "3.1.2",
10+
"version": "3.2.0",
1111
"repository": {
1212
"type": "git",
1313
"url": "https://github.com/patrickroberts/complex-js.git"

0 commit comments

Comments
 (0)