Repository: josdejong/mathjs
Pull request #862 (2017-05-24 03:41:52)

Fixed Bug in Equation For Intersection of Line and Plane
The equation to find the value of ’t' for the intersection of a line and a plane does not appear to be entirely correct.  Please double-check.


Patch:
diff --git a/HISTORY.md b/HISTORY.md
index f86e04779..d27259ed7 100644
--- a/HISTORY.md
+++ b/HISTORY.md
@@ -1,11 +1,5 @@
 # History
 
-## not yet released, version 3.13.2
-
-- Fixed #856: function `subset` not returning non-primitive scalars
-  from Arrays correctly. (like `math.eval('arr[1]', {arr: [math.bignumber(2)]})`.
-
-
 ## 2017-05-12, version 3.13.1
 
 - Fixed creating units with an alias not working within the expression
diff --git a/lib/expression/node/utils/access.js b/lib/expression/node/utils/access.js
index 6cf38ad70..671209eaa 100644
--- a/lib/expression/node/utils/access.js
+++ b/lib/expression/node/utils/access.js
@@ -5,6 +5,7 @@ var getSafeProperty = require('../../../utils/customs').getSafeProperty;
 
 function factory (type, config, load, typed) {
   var subset = load(require('../../../function/matrix/subset'));
+  var matrix = load(require('../../../type/matrix/function/matrix'));
 
   /**
    * Retrieve part of an object:
@@ -20,7 +21,7 @@ function factory (type, config, load, typed) {
   return function access(object, index) {
     try {
       if (Array.isArray(object)) {
-        return subset(object, index);
+        return matrix(object).subset(index).valueOf();
       }
       else if (object && typeof object.subset === 'function') { // Matrix
         return object.subset(index);
diff --git a/lib/function/geometry/intersect.js b/lib/function/geometry/intersect.js
index 1ce3fa18a..ad2be7e55 100644
--- a/lib/function/geometry/intersect.js
+++ b/lib/function/geometry/intersect.js
@@ -125,7 +125,7 @@ function factory (type, config, load, typed) {
   }
 
   function _intersectLinePlane(x1, y1, z1, x2, y2, z2, x, y, z, c){
-    var t = (c - x1*x - y1*y - z1*z)/(x2*x + y2*y + z2*z - x1 - y1 - z1);
+    var t = (c - x1*x - y1*y - z1*z)/(x2*x + y2*y + z2*z - x1*x - y1*y - z1*z);
     var px = x1 + t * (x2 - x1);
     var py = y1 + t * (y2 - y1);
     var pz = z1 + t * (z2 - z1);
diff --git a/lib/function/matrix/subset.js b/lib/function/matrix/subset.js
index 3113e961e..d71cd9a66 100644
--- a/lib/function/matrix/subset.js
+++ b/lib/function/matrix/subset.js
@@ -48,9 +48,7 @@ function factory (type, config, load, typed) {
     'Array, Index': function (value, index) {
       var m = matrix(value);
       var subset = m.subset(index);       // returns a Matrix
-      return index.isScalar()
-          ? subset
-          : subset.valueOf();  // return an Array (like the input)
+      return subset && subset.valueOf();  // return an Array (like the input)
     },
 
     'Matrix, Index': function (value, index) {


Test patch:
diff --git a/test/expression/parse.test.js b/test/expression/parse.test.js
index 029366824..25bbeaf17 100644
--- a/test/expression/parse.test.js
+++ b/test/expression/parse.test.js
@@ -489,11 +489,6 @@ describe('parse', function() {
       assert.deepEqual(parseAndEval('a[2, :][1,1]', scope), 4);
     });
 
-    it('should get BigNumber value from an array', function() {
-      var res = parseAndEval('arr[1]', {arr: [math.bignumber(2)]});
-      assert.deepEqual(res, math.bignumber(2));
-    });
-
     it('should parse matrix resizings', function() {
       var scope = {};
       assert.deepEqual(parseAndEval('a = []', scope),    math.matrix([]));
diff --git a/test/function/geometry/intersect.test.js b/test/function/geometry/intersect.test.js
index c1a6b4790..f1964cf4f 100644
--- a/test/function/geometry/intersect.test.js
+++ b/test/function/geometry/intersect.test.js
@@ -21,6 +21,12 @@ describe('intersect', function() {
     assert.deepEqual(math.intersect(math.matrix([1, 0, 1]), [4, -2, 2], math.matrix([1, 1, 1, 6])), math.matrix([7, -4, 3]));
     assert.deepEqual(math.intersect(math.matrix([1, 0, 1]), math.matrix([4, -2, 2]), math.matrix([1, 1, 1, 6])), math.matrix([7, -4, 3]));
   });
+
+  it('should calculate the intersection point of a line and a plane with plane coefficients other than (1, 1, 1)', function() {
+    assert.deepEqual(math.intersect([0, 30, 0], [0, 21, 0], [0, 9, 0, 0]), [0, 0, 0]);
+    assert.deepEqual(math.intersect(math.matrix([0, 30, 0]), [0, 21, 0], math.matrix([0, 9, 0, 0])), math.matrix([0, 0, 0]));
+    assert.deepEqual(math.intersect(math.matrix([0, 30, 0]), math.matrix([0, 21, 0]), math.matrix([0, 9, 0, 0])), math.matrix([0, 0, 0]));
+  });
   
   it('should return null if the points do not intersect', function() {
     assert.deepEqual(math.intersect([0, 1, 0], [0, 0, 0], [1, 1, 0], [1, 0, 0]), null);
diff --git a/test/function/matrix/subset.test.js b/test/function/matrix/subset.test.js
index b0b88b457..58e9fba5f 100644
--- a/test/function/matrix/subset.test.js
+++ b/test/function/matrix/subset.test.js
@@ -14,7 +14,6 @@ describe('subset', function() {
   it('should get the right subset of an array', function() {
     assert.deepEqual(subset(a, index(new Range(0,2), 1)), [[2],[4]]);
     assert.deepEqual(subset(a, index(1,0)), 3);
-    assert.deepEqual(subset([math.bignumber(2)], index(0)), math.bignumber(2));
   });
 
   it('should throw an error if trying to access an invalid subset of an array', function() {