bigrat
Version:
rational.js: tools and libraries using rational numbers.
370 lines (280 loc) • 7.54 kB
HTML
<html lang="en-ca">
<head>
<meta charset="utf-8">
<title>rational.js browser tester</title>
<!-- the latest release, replaced with the dev versions below if they exist -->
<script src="http://acerix.github.com/rational.js/releases/rationaljs.alpha.js"></script>
<script src="../src/integer.js"></script>
<script src="../src/factorial.js"></script>
<script src="../src/biginteger.js"></script>
<script src="../src/bigint.js"></script>
<script src="../src/rat.js"></script>
<script src="../src/bigrat.js"></script>
<script src="../src/rational.js"></script>
<script src="../src/alpha.js"></script>
<script src="../src/physics.js"></script>
<script src="../src/physics.planets.js"></script>
<script src="../src/chemistry.js"></script>
<script src="../src/chemistry.elements.js"></script>
<script src="../src/polyrat.js"></script>
<script src="../src/polyrat.examples.js"></script>
<script src="../src/deprecated.js"></script>
<script>
var input, output;
window.onload = function() {
input = document.getElementById('input');
input.focus();
output = document.getElementById('output');
go();
}
function go() {
eval('{'+input.value+'}');
}
function clr() {
output.innerHTML = '';
}
function print(s) {
if (typeof s !== 'undefined') output.innerHTML += s.toString();
output.innerHTML += '\n';
}
var alt_down = false, shift_down = false, ctrl_down = false;
function insertTextAtCursor(text) {
var sel, range, html;
if (window.getSelection) {
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
range.insertNode( document.createTextNode(text) );
}
}
else if (document.selection && document.selection.createRange) {
document.selection.createRange().text = text;
}
}
window.onkeydown = function(e) {
if (!e) e=event;
var k = e.which;
switch (k) {
case 9: // tab
e.preventDefault();
insertTextAtCursor('\t');
break;
case 13: // enter
if (alt_down) clr();
if (alt_down||ctrl_down) go();
break;
case 16: // shift
shift_down = true;
break;
case 17: // ctrl
ctrl_down = true;
break;
case 18: // alt
alt_down = true;
break;
// default: print('key down: '+k);
}
}
window.onkeyup = function(e) {
//clr(); go();
var k = (e?e:event).which;
switch (k) {
case 16: // shift
shift_down = false;
break;
case 17: // ctrl
ctrl_down = false;
break;
case 18: // alt
alt_down = false;
break;
}
}
function dumpStack() {
try {fake+=0;}
catch(e) {console.log(e.stack);}
}
</script>
</head>
<body>
<textarea id="input" style="width:99%;padding:3px 10px;" rows="24">
var r = bigrat.fromRandom(bigrat.create());
print(bigrat.dump(r));
print(bigrat.dump(bigrat.fromDecimal(bigrat.toDecimal(r))));
print();
var a = polyrat.fromValues(
[0, 0, 7, 1, 2, 3, 81, 4, 23]
);
var b = polyrat.fromValues(
[1, 2, 1, 0, 0, 3, 69]
);
var result = polyrat.create();
//print(polyrat.toJSON(a));
print(polyrat.str(a));
print(' + ');
//print(polyrat.toJSON(b));
print(polyrat.str(b));
print(' = ');
polyrat.add(result, a, b);
//print(polyrat.toJSON(result));
print(polyrat.str(result));
print();
print('@'+r[0]+' => '+bigrat.str(polyrat.evaluate(bigrat.create(), result, [r[0]])));
exit(); /* remove this to test the rest */
print();
var x2 = polyrat.fromValues([0, 0, 1]);
print(polyrat.toJSON(x2));
print(polyrat.str(x2));
print('@[x=6] => '+bigrat.str(polyrat.evaluate(bigrat.create(), x2, [6])));
print();
var x2_y2 = polyrat.fromValues([
[0, 0, 1],
[0, 0, 0],
[1, 0, 0]
]);
print(polyrat.toJSON(x2_y2));
print(polyrat.str(x2_y2));
print('@[x=6, y=9] => '+bigrat.str(polyrat.evaluate(bigrat.create(), x2_y2, [6, 9])));
print();
print(polyrat.toJSON(polyrat.PARABOLA_INVERSE));
print(polyrat.str(polyrat.PARABOLA_INVERSE));
print('@[x=7, y=11] => '+bigrat.str(polyrat.evaluate(bigrat.create(), polyrat.PARABOLA_INVERSE, [7, 11])));
print();
var x4_y4_z4 = polyrat.fromValues([
[
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 0, 0, 0, 0]
],
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
],
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
],
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
],
[
[1, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
]
]);
print(polyrat.toJSON(x4_y4_z4));
print(polyrat.str(x4_y4_z4));
print('@[x=5, y=9, z=3] => '+bigrat.str(polyrat.evaluate(bigrat.create(), x4_y4_z4, [5,9,3])));
print();
var PN_Fermat = [
[-1, 0, 0, 1],
[0],
[0],
[1]
];
var fermat = polyrat.fromValues(PN_Fermat);
print(polyrat.toJSON(fermat));
print(polyrat.str(fermat));
print('@[x=5, y=9] => '+bigrat.str(polyrat.evaluate(bigrat.create(), fermat, [5,9])));
print();
var crazy_polynomial = polyrat.fromValues([
[
[7, 6, 3, 0, 1],
[0, 0, 4, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0],
[1, 0, 0, 0, 0]
],
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 6, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
],
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 5, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 4, 0, 0]
],
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
],
[
[1, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 4, 0, 0],
[1, 0, 0, 5, 99]
]
]);
crazy_polynomial[1][2] = 24;
crazy_polynomial[1][1] = -9;
print(polyrat.toJSON(crazy_polynomial));
print(polyrat.str(crazy_polynomial));
print('@[x=5, y=9, z=3] => '+bigrat.str(polyrat.evaluate(bigrat.create(), crazy_polynomial, [5,9,3])));
print('');
print(new rational(420, 69).times(new rational(123, 456)).pow(4).divide(new rational(2446, 445)).divide(new rational(22, 33)).dump());
print(bigrat.dump(bigrat.fromContinuedFraction(bigrat.create(),
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
)));
print(bigrat.dump(bigrat.fromContinuedFraction(bigrat.create(),
[2,1,2,1,1,4,1,1,6,1,1,8,1,1,1,10,1,1,1,12,1,1,1,14,1,1,1,16,1,1,1,18,1,1,1,20,1,1,1,22,1,1,1,24]
)));
print(rational.fromDecimal(0.697774657964008).dump());
var nth = 2;
var root = bigrat.fromInteger(2);
for (var n=1; n<30; n++) {
print('nthRoot('+n+', '+nth+')');
bigrat.nthRoot(root, bigrat.fromInteger(n), nth);
print(bigrat.dump(root));
}
var PHI = ( 1 + Math.sqrt(5) ) / 2;
print(bigrat.dump(bigrat.fromDecimal(PHI)));
print(bigrat.dump(bigrat.fromDecimal(PHI - 1)));
bigrat.PHI = bigrat.fromInteger(5);
bigrat.sqrt(bigrat.PHI, bigrat.clone(bigrat.PHI));
bigrat.add(bigrat.PHI, bigrat.clone(bigrat.PHI), bigrat.ONE);
bigrat.scalar_divide(bigrat.PHI, bigrat.clone(bigrat.PHI), 2);
print(bigrat.dump(bigrat.PHI));
print(bigrat.dump(bigrat.fromDecimal(Math.PI)));
print(rat.dump(rat.PI));
print(bigrat.dump(bigrat.PI));
print(bigrat.dump(physics.a));
print(bigrat.dump(bigrat.invert(bigrat.create(), physics.a)));
print(bigrat.dump(physics.h));
print(bigrat.dump(physics.hBAR));
print(bigrat.dump(bigrat.fromDecimal(Math.E)));
print(bigrat.dump(bigrat.fromDecimal(Math.LN2)));
print(bigrat.dump(bigrat.fromDecimal(Math.LOG2E)));
print(bigrat.dump(bigrat.fromDecimal(Math.LN10)));
print(bigrat.dump(bigrat.fromDecimal(Math.LOG10E)));
</textarea>
<br />
<input type="button" value="go" onclick="clr();go();" style="width:98%;margin:10px;float:right;">
<pre id="output" style="width:99%;border:1px black outset;clear:both;padding:10px;"></pre>
</body>
</html>