Intial Commit

This commit is contained in:
valki
2020-10-17 18:42:50 +02:00
commit 664c6d8ca3
5892 changed files with 759183 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
/**
* @license Fraction.js v2.7.0 01/06/2015
* http://www.xarg.org/2014/03/rational-numbers-in-javascript/
*
* Copyright (c) 2015, Robert Eisele (robert@xarg.org)
* Dual licensed under the MIT or GPL Version 2 licenses.
**/
// Another rational approximation, not using Farey Sequences but Binary Search using the mediant
function approximate(p, precision) {
var num1 = Math.floor(p);
var den1 = 1;
var num2 = num1 + 1;
var den2 = 1;
if (p !== num1) {
while (den1 <= precision && den2 <= precision) {
var m = (num1 + num2) / (den1 + den2);
if (p === m) {
if (den1 + den2 <= precision) {
den1 += den2;
num1 += num2;
den2 = precision + 1;
} else if (den1 > den2) {
den2 = precision + 1;
} else {
den1 = precision + 1;
}
break;
} else if (p < m) {
num2 += num1;
den2 += den1;
} else {
num1 += num2;
den1 += den2;
}
}
}
if (den1 > precision) {
den1 = den2;
num1 = num2;
}
return new Fraction(num1, den1);
}

View File

@@ -0,0 +1,23 @@
/**
* @license Fraction.js v2.7.0 01/06/2015
* http://www.xarg.org/2014/03/rational-numbers-in-javascript/
*
* Copyright (c) 2015, Robert Eisele (robert@xarg.org)
* Dual licensed under the MIT or GPL Version 2 licenses.
**/
// Based on http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fractions/egyptian.html
function egyptian(a, b) {
var res = [];
do {
var t = Math.ceil(b / a);
var x = new Fraction(a, b).sub(1, t);
res.push(t);
a = x.n;
b = x.d;
} while (a !== 0);
return res;
}
console.log("1 / " + egyptian(521, 1050).join(" + 1 / "));

View File

@@ -0,0 +1,112 @@
/**
* @license Fraction.js v2.4.1 01/06/2015
* http://www.xarg.org/2014/03/rational-numbers-in-javascript/
*
* Copyright (c) 2015, Robert Eisele (robert@xarg.org)
* Dual licensed under the MIT or GPL Version 2 licenses.
**/
var Fraction = require('../fraction.min.js');
/*
We have the polynom f(x) = 1/3x_1^2 + x_2^2 + x_1 * x_2 + 3
The gradient of f(x):
grad(x) = | x_1^2+x_2 |
| 2x_2+x_1 |
And thus the Hesse-Matrix H:
| 2x_1 1 |
| 1 2 |
The inverse Hesse-Matrix H^-1 is
| -2 / (1-4x_1) 1 / (1 - 4x_1) |
| 1 / (1 - 4x_1) -2x_1 / (1 - 4x_1) |
We now want to find lim ->oo x[n], with the starting element of (3 2)^T
*/
// Get the Hesse Matrix
function H(x) {
var z = new Fraction(1).sub(new Fraction(4).mul(x[0]));
return [
new Fraction(-2).div(z),
new Fraction(1).div(z),
new Fraction(1).div(z),
new Fraction(-2).mul(x[0]).div(z),
];
}
// Get the gradient of f(x)
function grad(x) {
return [
new Fraction(x[0]).mul(x[0]).add(x[1]),
new Fraction(2).mul(x[1]).add(x[0])
];
}
// A simple matrix multiplication helper
function matrMult(m, v) {
return [
new Fraction(m[0]).mul(v[0]).add(new Fraction(m[1]).mul(v[1])),
new Fraction(m[2]).mul(v[0]).add(new Fraction(m[3]).mul(v[1]))
];
}
// A simple vector subtraction helper
function vecSub(a, b) {
return [
new Fraction(a[0]).sub(b[0]),
new Fraction(a[1]).sub(b[1])
];
}
// Main function, gets a vector and the actual index
function run(V, j) {
var t = H(V);
//console.log("H(X)");
for (var i in t) {
// console.log(t[i].toFraction());
}
var s = grad(V);
//console.log("vf(X)");
for (var i in s) {
// console.log(s[i].toFraction());
}
//console.log("multiplikation");
var r = matrMult(t, s);
for (var i in r) {
// console.log(r[i].toFraction());
}
var R = (vecSub(V, r));
console.log("X"+j);
console.log(R[0].toFraction(), "= "+R[0].valueOf());
console.log(R[1].toFraction(), "= "+R[1].valueOf());
console.log("\n");
return R;
}
// Set the starting vector
var v = [3, 2];
for (var i = 0; i < 15; i++) {
v = run(v, i);
}

View File

@@ -0,0 +1,68 @@
/**
* @license Fraction.js v2.7.0 01/06/2015
* http://www.xarg.org/2014/03/rational-numbers-in-javascript/
*
* Copyright (c) 2015, Robert Eisele (robert@xarg.org)
* Dual licensed under the MIT or GPL Version 2 licenses.
**/
// NOTE: This is a nice example, but a stable version of this is served with Polynomial.js:
// https://github.com/infusion/Polynomial.js
var Fraction = require('../fraction.min.js');
function integrate(poly) {
poly = poly.replace(/\s+/g, "");
var regex = /(\([+-]?[0-9/]+\)|[+-]?[0-9/]+)x(?:\^(\([+-]?[0-9/]+\)|[+-]?[0-9]+))?/g;
var arr;
var res = {};
while (null !== (arr = regex.exec(poly))) {
var a = (arr[1] || "1").replace("(", "").replace(")", "").split("/");
var b = (arr[2] || "1").replace("(", "").replace(")", "").split("/");
var exp = new Fraction(b).add(1);
var key = "" + exp;
if (res[key] !== undefined) {
res[key] = {x: new Fraction(a).div(exp).add(res[key].x), e: exp};
} else {
res[key] = {x: new Fraction(a).div(exp), e: exp};
}
}
var str = "";
var c = 0;
for (var i in res) {
if (res[i].x.s !== -1 && c > 0) {
str += ("+");
} else if (res[i].x.s === -1) {
str += ("-");
}
if (res[i].x.n / res[i].x.d !== 1) {
if (res[i].x.d !== 1) {
str += ("" + res[i].x.n + "/" + res[i].x.d + "");
} else {
str += ("" + res[i].x.n);
}
}
str += ("x");
if (res[i].e.n / res[i].e.d !== 1) {
str += ("^");
if (res[i].e.d !== 1) {
str += ("(" + res[i].e.n + "/" + res[i].e.d + ")");
} else {
str += ("" + res[i].e.n);
}
}
c++;
}
return str;
}
var poly = "-2/3x^3-2x^2+3x+8x^3-1/3x^(4/8)";
console.log("f(x): " + poly);
console.log("F(x): " + integrate(poly));