Skip to content

Commit 1b53f9d

Browse files
committed
clone()n and expand() for collections and vectors
1 parent 49e1e7b commit 1b53f9d

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

all.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nerdamer.core.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -4857,6 +4857,15 @@ var nerdamer = (function (imports) {
48574857
collection.append(e);
48584858
return collection;
48594859
};
4860+
Collection.prototype.clone = function (elements) {
4861+
const c = Collection.create();
4862+
c.elements = this.elements.map((e)=>e.clone());
4863+
return c;
4864+
};
4865+
Collection.prototype.expand = function (options) {
4866+
this.elements = this.elements.map((e)=>_.expand(e, options));
4867+
return this;
4868+
};
48604869

48614870
function Token(node, node_type, column) {
48624871
this.type = node_type;
@@ -8525,14 +8534,17 @@ var nerdamer = (function (imports) {
85258534
return expand(x, opt);
85268535
});
85278536
}
8537+
if (symbol.expand) {
8538+
return symbol.expand(opt);
8539+
}
85288540
opt = opt || {};
85298541
//deal with parenthesis
85308542
if(symbol.group === FN && symbol.fname === '') {
85318543
var f = expand(symbol.args[0], opt);
85328544
var x = expand(_.pow(f, _.parse(symbol.power)), opt);
85338545
return _.multiply(_.parse(symbol.multiplier), x).distributeMultiplier();
85348546
}
8535-
// We can expand these groups so no need to waste time. Just return and be done.
8547+
// We cannot expand these groups so no need to waste time. Just return and be done.
85368548
if([N, P, S].indexOf(symbol.group) !== -1) {
85378549
return symbol; //nothing to do
85388550
}
@@ -11224,6 +11236,11 @@ var nerdamer = (function (imports) {
1122411236
return V;
1122511237
},
1122611238

11239+
expand: function (options) {
11240+
this.elements = this.elements.map((e)=>_.expand(e, options));
11241+
return this;
11242+
},
11243+
1122711244
// Maps the vector to another vector according to the given function
1122811245
map: function (fn) {
1122911246
var elements = [];

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"description": "javascript light-weight symbolic math library",
77
"name": "nerdamer-prime",
88
"license": "MIT",
9-
"version": "1.1.24",
9+
"version": "1.1.25",
1010
"homepage": "https://github.com/together-science/nerdamer-prime",
1111
"directory": {
1212
"lib": "./"

spec/core.spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,12 @@ describe('Nerdamer core', function () {
18071807
expect(nerdamer('[21,15]/3').toString()).toEqual('[7,5]');
18081808
expect(nerdamer('[a^2, b^2]/[a,b]').toString()).toEqual('[a,b]');
18091809
});
1810+
it('should expand vectors correctly', function() {
1811+
expect(nerdamer('[a*(b+c),4]').expand().toString()).toEqual('[a*b+a*c,4]');
1812+
});
1813+
it('should expand collections correctly', function() {
1814+
expect(nerdamer('(a*(b+c),4)').expand().toString()).toEqual('(a*b+a*c, 4)');
1815+
});
18101816
it('should get slices correctly', function() {
18111817
expect(nerdamer('[1,2,3][0:2]').toString()).toEqual('[1,2]');
18121818
});

spec/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ try {
4444
// console.log(x.text());
4545
// return;
4646
// x= nerdamer("-sqrt(8/12)")
47-
x= nerdamer("simplify(cos(a+b))");
47+
x= nerdamer("[3*(x+4),4]").expand();
4848
console.log(x.text());
4949
} catch (error) {
5050
console.log("error "+error)

0 commit comments

Comments
 (0)