Skip to content

Commit bf8522d

Browse files
committed
vector cross dot fix
1 parent a0d4c00 commit bf8522d

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

nerdamer.core.js

+21
Original file line numberDiff line numberDiff line change
@@ -8869,12 +8869,26 @@ var nerdamer = (function (imports) {
88698869
}
88708870

88718871
function dot(vec1, vec2) {
8872+
if (isMatrix(vec1)) {
8873+
vec1 = new Vector(vec1);
8874+
}
8875+
if (isMatrix(vec2)) {
8876+
vec2 = new Vector(vec2);
8877+
}
8878+
88728879
if(isVector(vec1) && isVector(vec2))
88738880
return vec1.dot(vec2);
88748881
err('function dot expects 2 vectors');
88758882
}
88768883

88778884
function cross(vec1, vec2) {
8885+
if (isMatrix(vec1)) {
8886+
vec1 = new Vector(vec1);
8887+
}
8888+
if (isMatrix(vec2)) {
8889+
vec2 = new Vector(vec2);
8890+
}
8891+
88788892
if(isVector(vec1) && isVector(vec2))
88798893
return vec1.cross(vec2);
88808894
err('function cross expects 2 vectors');
@@ -11190,6 +11204,13 @@ var nerdamer = (function (imports) {
1119011204
this.elements = v.items.slice(0);
1119111205
else if(isArray(v))
1119211206
this.elements = v.slice(0);
11207+
else if(isMatrix(v)) {
11208+
if (v.elements.length === 1) {
11209+
this.elements = [...v.elements[0]];
11210+
} else if (v.elements.length > 1 && Array.isArray(v.elements[0]) && v.elements[0].length === 1) {
11211+
this.elements = v.elements.map((row)=>row[0]);
11212+
}
11213+
}
1119311214
else
1119411215
this.elements = [].slice.call(arguments);
1119511216
}

spec/core.spec.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,7 @@ describe('Nerdamer core', function () {
17911791
expect(nerdamer('1+[a,b]').toString()).toEqual('[1+a,1+b]');
17921792
expect(nerdamer('[a,b]+1').toString()).toEqual('[1+a,1+b]');
17931793
expect(nerdamer('[a,b]+[a,b]').toString()).toEqual('[2*a,2*b]');
1794+
expect(nerdamer('[a,b]+[c,d]').expand().toString()).toEqual('[a+c,b+d]');
17941795
});
17951796
it('should subtract vectors correctly', function() {
17961797
expect(nerdamer('1-[a,b]').toString()).toEqual('[-a+1,-b+1]');
@@ -1810,18 +1811,9 @@ describe('Nerdamer core', function () {
18101811
it('should expand vectors correctly', function() {
18111812
expect(nerdamer('[a*(b+c),4]').expand().toString()).toEqual('[a*b+a*c,4]');
18121813
});
1813-
it('should add vectors correctly', function() {
1814-
expect(nerdamer('[a,b]+[c,d]').expand().toString()).toEqual('[a+c,b+d]');
1815-
});
18161814
it('should expand collections correctly', function() {
18171815
expect(nerdamer('(a*(b+c),4)').expand().toString()).toEqual('(a*b+a*c, 4)');
18181816
});
1819-
// it('should get slices correctly', function() {
1820-
// expect(nerdamer('[1,2,3][0:2]').toString()).toEqual('[1,2]');
1821-
// });
1822-
// it('should get elements correctly', function() {
1823-
// expect(nerdamer('[1,2,3][1]').toString()).toEqual('2');
1824-
// });
18251817
it('should remove near duplicates from vectors', function() {
18261818
expect(nerdamer('vectrim([cos(0), 1, 1.000000000000001])').toString()).toEqual('[1]');
18271819
expect(nerdamer('vectrim([cos(0), 1, 1.000000000000001], 0)').text()).toEqual('[1,1.000000000000001]');

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("[4,5]+[3,4]")
47+
x= nerdamer("cross(matrix([4,5,4]),matrix([3,4,4]))")
4848
console.log(x.text());
4949
} catch (error) {
5050
console.log("error "+error)

0 commit comments

Comments
 (0)