 |
Console application
This console application illustrates features of the
Jep Extensions
package.
Field:
This example has been cross compiled to javascript
using the GWTJep package.
Field selection
This console can perform calculations using various different fields including integers, fractions,
decimals with a specific number of decimal places. Use
setfield double for standard floating point numbers
setfield bigdec 30 for big decimal numbers with 30 decimal places
setfield bigdec unlimited for big decimal numbers with unlimited size
setfield integer all calculations in integer
setfield exactint all calculations in integers throwing exceptions on overflow
setfield bigint all calculations using unlimited precision BigIntegers
setfield rational for fractions (rational numbers unlimited precision)
setfield complex all calculations using complex numbers
setfield simple closest match to standard jep operation
setfield mixed 30 combines rational numbers and bigdecimal number with 30 dp
Notes:
- Changing the field resets all variables.
- Different fields have different sets of functions and constants defiend
- The JavaScript versions may not always give the same results as the Java version as JavaScript
has different ways of representing numbers.
Structured programming
The console allows simple structured programming constructs like loops and
if statments. It supports
for(i=1;i<10;++i) { ... }
while(i<10) { ... } while loops
break; (inside a loop)
continue; (inside a loop)
if(i<10) { ... } else { ... }
statement; statement
{ statement; statement }
print(a,b,c)
println(a,b,c)
Examples
A simple loop can add the numbers from 1 to 10
sum=0; for(i=1; i<=10; ++i) { sum += i; }
Symbolic operations
diff(x^2,x) differentiate x^2 with respect to x
rules prints the set of differentiation rules used
clean(0+1*x) cleans a expression
simplify(2x+3x) simplifies a expression using the polynomial simplification algorithm
expand((x+1)*(x-1)) expands a polynomial'
compare((x+1)^2, x^2+2x+1) compares without expansion
coeffs(x^2+2x+1,x) extract coefficients as an array
subst(x*y*z,y=x+1,z=x-1) substitution rhs equations into the lhs expression
Symbolic assignment
f := x^2 set f to have and equation from the rhs
eqn(f) extract equation from a symbolic variable
verbose on/verbose off switch verbose mode on or off
Matrix operations
m = [[1,2],[3,4]] - creation of matrix
u = [5,6] - creation of vector
m[1][2] - find the 2nd element in the 1st column
det(m) - the determinate of a matrix
trace(m) - the trace of a matrix
trans(m) - transpose of a matrix
id(3) - creates a 3x3 identity matrix
zeroMat(3,2) - creates a 3 by matrix of zeros
zeroVec(3) - creates a vector of zeros length 3
size(m) - finds the length of a vector or size of a matrix
inv(m) - find the inverse of m
v=solve(m,u) - find solution of m * v = u
Statistical functions
count(v) - count the number of elements
min(v) - finds the min value in a vector or matrix
max(v) - finds the max value in a vector or matrix
sum(v) - finds the sum of the elements
product(v) - finds the product of the elements
mean(v) - finds the mean value in a vector or matrix
var(v) - finds the variance of the values
sd(v) - finds the standard deviation of the values
median(v) - finds the mean value in a vector or matrix
ranks(d,data) - finds the rank of element d in data array
ranks(v) - finds the ranks all the elements
mode(v) - finds the mode of the values
Advanced Examples
Using the factorial(x) function to find number of digits of precision
Input
setfield integer
factorial(10)
factorial(20)
factorial(21)
factorial(22)
|
Result
Setting field INTEGER
3628800
2432902008176640000
51090942171709440000
1.1240007277776077e+21
|
Doubles work the same
setfield double
factorial(21)
factorial(22)
|
Setting field DOUBLE
51090942171709440000
1.1240007277776077e+21
|
BigIntegers allow much larger values
setfield bigint
factorial(20)
factorial(30)
factorial(40)
factorial(50)
|
Setting field BIGINT
2432902008176640000
265252859812191058636308480000000
815915283247897734345611269596115894272000000000
30414093201713378043612608166064768844377641568960512000000000000
|
Calculations with fractions
setfield rational
1/6*2/5
1/6+1/2
|
Setting field RATIONAL
2/3
1/15
|
Calculation of pi using Ramanujan's formula
s=1103; a =1; c=1; d=1; \\
for(k=1;k<10;++k) {\\
a*=(4*k-3)*(4*k-2)*(4*k-1)*(4*k); \\
b =1103 + 26390*k; \\
c *= k*k*k*k; d *= 396^4; s+= a*b/(c*d); \\
v = 9801/(2*sqrt(2)*s); println(v); }
|
3.141592653589793877998905826306015
3.141592653589793238462649065702759
3.141592653589793238462643383279558
3.141592653589793238462643383279506
3.141592653589793238462643383279506
3.141592653589793238462643383279506
3.141592653589793238462643383279506
3.141592653589793238462643383279506
3.141592653589793238462643383279506
|
Calculation of e
s=1; f=1; for(k=1;k<30;++k) { f*=k; s+=1/f; println(s) }
|
2
2.5
2.666666666666666666666666666666667
2.708333333333333333333333333333334
2.716666666666666666666666666666667
2.718055555555555555555555555555556
2.718253968253968253968253968253969
...
2.718281828459045235360287471352545
2.718281828459045235360287471352658
|
Continued fraction for pi
setfield double
a=zeroVec(20);
n=pi; for(i=1;i<=20;++i) { b=floor(n); n = 1/(n-b); a[i]=b }
a
|
Setting field DOUBLE
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
4
[3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 3, 3, 23, 1, 1, 7, 4]
|
Reconstructing values from array representation of a continued fraction
a=[3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 2, 1, 1, 2, 2, 2, 2]
for(i=1;i<=20;++i) { \\
s=a[i]; \\
for(j=i-1;j>0;--j) { \\
s = a[j]+1/s } \\
println(s) }
|
[3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 2, 1, 1, 2, 2, 2, 2]
3
3.142857142857142857142857142857143
3.141509433962264150943396226415094
...
3.141592653589793239014009759199591
3.141592653589793238386377506390380
3.141592653589793238493875058011561
|
Solving an expression using Newton method. Uses symbolic differentiation and symbolic
assignment f:= ...
f := x^2 - x - 1
g := diff(f,x)
x=1
for(i=0;i<10;++i) { x -= f/g; println(x,f); }
|
f:=x^2-x-1
g:=2*x-1
x=1
2, 1
1.666666666666666666666666666666667, 0.111111111111111111111111111111112
1.619047619047619047619047619047619, 0.002267573696145124716553287981859
1.618034447821681864235055724417427, 0.000001026515933067055100295739241
1.618033988749989097047296779290725, 2.10746819100131229750E-13
1.618033988749894848204586838338167, 8.882845E-27
1.618033988749894848204586834365638, 0E-33
1.618033988749894848204586834365638, 0E-33
1.618033988749894848204586834365638, 0E-33
1.618033988749894848204586834365638, 0E-33
|
|
 |