Skip to content

Commit

Permalink
Added optimization to compiler and bumped Copyright
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickroberts committed Apr 25, 2016
1 parent 98fb7f6 commit 4e0cc53
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 15 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2015 Patrick Roberts
Copyright (c) 2016 Patrick Roberts

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ input.addEventListener('change', function () {

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

The following is an example where the compiler provides parameters
Expand Down Expand Up @@ -1076,3 +1076,27 @@ __Arguments__
* `string` - A human-readable `String` of a math expression to be compiled.
* `params` - An optional `Array[String]` of human-readable parameters to parse.
* `skipFormat` - An optional `Boolean` to determine whether to skip pre-formatting.

## License

Copyright (c) 2016 Patrick Roberts

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.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "complex-js",
"description": "Complex math for the browser and Node.js",
"version": "3.1.3",
"version": "3.2.0",
"authors": [
"Patrick Roberts"
],
Expand Down
46 changes: 36 additions & 10 deletions lib/complex.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2015 Patrick Roberts
* Copyright (c) 2016 Patrick Roberts
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -180,13 +180,13 @@

if(Math.sinh === undefined) {
Math.sinh = function(x) {
return (-Math.exp(-x) + Math.exp(x)) / 2;
return (Math.exp(x) - Math.exp(-x)) / 2;
};
}

if(Math.cosh === undefined) {
Math.cosh = function(x) {
return (Math.exp(-x) + Math.exp(x)) / 2;
return (Math.exp(x) + Math.exp(-x)) / 2;
};
}

Expand All @@ -210,6 +210,8 @@
*/

function Complex(r, i, m, t) {
i = i || 0;

if(!(this instanceof Complex)) {
return new Complex(r, i, m, t);
}
Expand Down Expand Up @@ -1254,7 +1256,7 @@
throw new SyntaxError('"' + arg + '" is an invalid variable name.');
}

return name.toLowerCase();
return name;
});

for(i = 0; i < args.length; i++) {
Expand Down Expand Up @@ -1454,7 +1456,19 @@
// ignores extraneous grouping
str += (func === '(' ? '' : func);
i += match.length;
str += compile(exp.substring(i, j), vars, namespace) + (func === '(' ? '' : ')');
// efficiently compiles subgroup by first assuming a constant expression
try {
var ns = new Namespace();
var body = compile(exp.substring(i, j), [], ns, true);
var comp = (new Function('', 'var C=this.Complex;return ' + body + ';')).bind(ns)();
str += 'this[' + namespace.push(comp) + ']';
// then compiling with specified variables if that fails
} catch(e) {
str += compile(exp.substring(i, j), vars, namespace, true);
} finally {
str += (func === '(' ? '' : ')');
}

i = (exp[j] === ')' ? j + 1 : j);
findOper = true;
}
Expand Down Expand Up @@ -1495,7 +1509,19 @@

str += oper;
i += match.length;
str += compile(exp.substring(i, j), vars, namespace) + ')';
// efficiently compiles subgroup by first assuming a constant expression
try {
var ns = new Namespace();
var body = compile(exp.substring(i, j), [], ns, true);
var comp = (new Function('', 'var C=this.Complex;return ' + body + ';')).bind(ns)();
str += 'this[' + namespace.push(comp) + ']';
// then compiling with specified variables if that fails
} catch(e) {
str += compile(exp.substring(i, j), vars, namespace, true);
} finally {
str += ')';
}

i = (exp[j] === ')' ? j + 1 : j);
findOper = true;
// no identifiable expressions left to parse
Expand All @@ -1522,11 +1548,10 @@
if(typeof args === 'boolean') {
skipFormat = args;
args = [];
} else if(!(typeof args === 'object' && args !== null && args.constructor === Array)) {
} else if(!Array.isArray(args)) {
args = [];
}
// make everything lowercase because it's easier
str = str.toLowerCase();

// default is to format the human-readable string
if(!skipFormat) {
str = Complex.formatFunction(str);
Expand All @@ -1538,11 +1563,12 @@
// create namespace to bind to function
namespace = new Namespace(),
// compile function body from string given valid arguments and namespace to cache parsed numerical constants
body = compile(str, vars, namespace),
body = compile('(' + str + ')', vars, namespace),
// convert compiled variable names map to array for function arguments
params = args.map(function(arg) {
return vars[arg];
}).join(',');

// the constructed function being returned
return (new Function(params, 'var C=this.Complex;return ' + body + ';')).bind(namespace);
};
Expand Down
2 changes: 1 addition & 1 deletion lib/complex.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"author": "Patrick Roberts",
"main": "./index",
"version": "3.1.2",
"version": "3.2.0",
"repository": {
"type": "git",
"url": "https://github.com/patrickroberts/complex-js.git"
Expand Down

0 comments on commit 4e0cc53

Please sign in to comment.