[Pkg-javascript-commits] [science.js] 38/87: Add science.quadratic.

bhuvan krishna bhuvan-guest at moszumanska.debian.org
Thu Dec 8 06:11:56 UTC 2016


This is an automated email from the git hooks/post-receive script.

bhuvan-guest pushed a commit to branch master
in repository science.js.

commit 20d9989fed9fc1e3eb153d95a303a4273d24c0d1
Author: Jason Davies <jason at jasondavies.com>
Date:   Sun Aug 28 00:37:27 2011 +0100

    Add science.quadratic.
---
 Makefile                    |  1 +
 science.js                  | 33 +++++++++++++++++++++++++++++++++
 science.min.js              |  2 +-
 src/core/quadratic.js       | 33 +++++++++++++++++++++++++++++++++
 test/core/quadratic-test.js | 33 +++++++++++++++++++++++++++++++++
 5 files changed, 101 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index 7678670..7627532 100644
--- a/Makefile
+++ b/Makefile
@@ -21,6 +21,7 @@ science.core.js: \
 	src/core/expm1.js \
 	src/core/functor.js \
 	src/core/hypot.js \
+	src/core/quadratic.js \
 	src/core/zeroes.js
 
 science.vector.js: \
diff --git a/science.js b/science.js
index e760260..cf7eed0 100644
--- a/science.js
+++ b/science.js
@@ -18,6 +18,39 @@ science.hypot = function(x, y) {
   var r = min / max;
   return max * Math.sqrt(1 + r * r);
 };
+science.quadratic = function() {
+  var complex = false;
+
+  function quadratic(a, b, c) {
+    var d = b * b - 4 * a * c;
+    if (d > 0) {
+      d = Math.sqrt(d) / (2 * a);
+      return complex
+        ? [{r: -b - d, i: 0}, {r: -b + d, i: 0}]
+        : [-b - d, -b + d];
+    } else if (d === 0) {
+      d = -b / (2 * a);
+      return complex ? [{r: d, i: 0}] : [d];
+    } else {
+      if (complex) {
+        d = Math.sqrt(-d) / (2 * a);
+        return [
+          {r: -b, i: -d},
+          {r: -b, i: d}
+        ];
+      }
+      return [];
+    }
+  }
+
+  quadratic.complex = function(x) {
+    if (!arguments.length) return complex;
+    complex = x;
+    return quadratic;
+  };
+
+  return quadratic;
+};
 // Constructs a multi-dimensional array filled with zeroes.
 science.zeroes = function(n) {
   var i = -1,
diff --git a/science.min.js b/science.min.js
index b4edbed..1ca449e 100644
--- a/science.min.js
+++ b/science.min.js
@@ -1 +1 @@
-(function(){science={version:"1.5.0"},science.expm1=function(a){return a<1e-5&&a>-0.00001?a+.5*a*a:Math.exp(a)-1},science.functor=function(a){return typeof a=="function"?a:function(){return a}},science.hypot=function(a,b){a=Math.abs(a),b=Math.abs(b);var c,d;a>b?(c=a,d=b):(c=b,d=a);var e=d/c;return c*Math.sqrt(1+e*e)},science.zeroes=function(a){var b=-1,c=[];if(arguments.length===1)while(++b<a)c[b]=0;else while(++b<a)c[b]=science.zeroes.apply(this,Array.prototype.slice.call(arguments,1)); [...]
\ No newline at end of file
+(function(){science={version:"1.5.0"},science.expm1=function(a){return a<1e-5&&a>-0.00001?a+.5*a*a:Math.exp(a)-1},science.functor=function(a){return typeof a=="function"?a:function(){return a}},science.hypot=function(a,b){a=Math.abs(a),b=Math.abs(b);var c,d;a>b?(c=a,d=b):(c=b,d=a);var e=d/c;return c*Math.sqrt(1+e*e)},science.quadratic=function(){function b(b,c,d){var e=c*c-4*b*d;if(e>0){e=Math.sqrt(e)/(2*b);return a?[{r:-c-e,i:0},{r:-c+e,i:0}]:[-c-e,-c+e]}if(e===0){e=-c/(2*b);return a?[{ [...]
\ No newline at end of file
diff --git a/src/core/quadratic.js b/src/core/quadratic.js
new file mode 100644
index 0000000..a59c1e9
--- /dev/null
+++ b/src/core/quadratic.js
@@ -0,0 +1,33 @@
+science.quadratic = function() {
+  var complex = false;
+
+  function quadratic(a, b, c) {
+    var d = b * b - 4 * a * c;
+    if (d > 0) {
+      d = Math.sqrt(d) / (2 * a);
+      return complex
+        ? [{r: -b - d, i: 0}, {r: -b + d, i: 0}]
+        : [-b - d, -b + d];
+    } else if (d === 0) {
+      d = -b / (2 * a);
+      return complex ? [{r: d, i: 0}] : [d];
+    } else {
+      if (complex) {
+        d = Math.sqrt(-d) / (2 * a);
+        return [
+          {r: -b, i: -d},
+          {r: -b, i: d}
+        ];
+      }
+      return [];
+    }
+  }
+
+  quadratic.complex = function(x) {
+    if (!arguments.length) return complex;
+    complex = x;
+    return quadratic;
+  };
+
+  return quadratic;
+};
diff --git a/test/core/quadratic-test.js b/test/core/quadratic-test.js
new file mode 100644
index 0000000..aec6606
--- /dev/null
+++ b/test/core/quadratic-test.js
@@ -0,0 +1,33 @@
+require("../../science");
+
+var vows = require("vows"),
+    assert = require("assert");
+
+var suite = vows.describe("science.quadratic");
+
+suite.addBatch({
+  "real roots": {
+    topic: function() {
+      return science.quadratic();
+    },
+    "no roots": function(quadratic) {
+      assert.deepEqual(quadratic(1, 1, 1), []);
+    },
+    "single root": function(quadratic) {
+      assert.deepEqual(quadratic(1, -2, 1), [1]);
+    },
+    "two roots": function(quadratic) {
+      assert.deepEqual(quadratic(1, 1, -2), [-2.5, .5]);
+    }
+  },
+  "complex roots": {
+    topic: function() {
+      return science.quadratic().complex(true);
+    },
+    "imaginary parts": function(quadratic) {
+      assert.deepEqual(quadratic(1, 0, 1), [{r: 0, i: -1}, {r: 0, i: 1}]);
+    }
+  }
+});
+
+suite.export(module);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/science.js.git



More information about the Pkg-javascript-commits mailing list