Add array printer (printa), enable matrix ops, add equation solver
This commit is contained in:
parent
5d2ce564dc
commit
243f782214
@ -69,6 +69,8 @@ import static net.apocalypselabs.symat.Main.API_URL;
|
||||
import org.matheclipse.core.eval.EvalUtilities;
|
||||
import org.matheclipse.core.interfaces.IExpr;
|
||||
import org.matheclipse.parser.client.math.MathException;
|
||||
import org.mozilla.javascript.NativeArray;
|
||||
import org.python.core.PyList;
|
||||
|
||||
/**
|
||||
* These functions are accessible from JavaScript.
|
||||
@ -244,6 +246,118 @@ public class Functions {
|
||||
return diff(function, idv);
|
||||
}
|
||||
|
||||
public double[] solve(String function, String idv, String eq) {
|
||||
String res = $("Solve[" + function + "==" + eq + ", " + idv + "]");
|
||||
res = res.substring(1, res.length() - 1);
|
||||
String[] cmp = res.split(",");
|
||||
for (int i = 0; i < cmp.length; i++) {
|
||||
cmp[i] = cmp[i].replace("{" + idv + "->", "");
|
||||
cmp[i] = cmp[i].replace("}", "");
|
||||
}
|
||||
double[] out = new double[cmp.length];
|
||||
for (int i = 0; i < cmp.length; i++) {
|
||||
try {
|
||||
out[i] = Double.parseDouble(cmp[i]);
|
||||
} catch (Exception ex) {
|
||||
Debug.stacktrace(ex);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
public double[] solve(String function, String idv) {
|
||||
return solve(function, idv, "0");
|
||||
}
|
||||
|
||||
public double[] solve(String function) {
|
||||
return solve(function, "x");
|
||||
}
|
||||
|
||||
private String printa(double[] o) {
|
||||
String out = "[";
|
||||
for (int i = 0; i < o.length; i++) {
|
||||
out += o[i] + (i == o.length - 1 ? "" : ", ");
|
||||
}
|
||||
out += "]";
|
||||
return out;
|
||||
}
|
||||
|
||||
private String printa(int[] o) {
|
||||
String out = "[";
|
||||
for (int i = 0; i < o.length; i++) {
|
||||
out += o[i] + (i == o.length - 1 ? "" : ", ");
|
||||
}
|
||||
out += "]";
|
||||
return out;
|
||||
}
|
||||
|
||||
private String printa(boolean[] o) {
|
||||
String out = "[";
|
||||
for (int i = 0; i < o.length; i++) {
|
||||
out += (o[i] ? "true" : "false") + (i == o.length - 1 ? "" : ", ");
|
||||
}
|
||||
out += "]";
|
||||
return out;
|
||||
}
|
||||
|
||||
public String printa(Object o) {
|
||||
String out = "[";
|
||||
if (o instanceof int[]) {
|
||||
int[] arr = (int[]) o;
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
out += arr[i] + (i == arr.length - 1 ? "" : ", ");
|
||||
}
|
||||
} else if (o instanceof double[]) {
|
||||
double[] arr = (double[]) o;
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
out += arr[i] + (i == arr.length - 1 ? "" : ", ");
|
||||
}
|
||||
} else if (o instanceof boolean[]) {
|
||||
double[] arr = (double[]) o;
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
out += arr[i] + (i == arr.length - 1 ? "" : ", ");
|
||||
}
|
||||
} else if (o instanceof int[][]) {
|
||||
int[][] arr = (int[][]) o;
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
out += printa(arr[i]) + (i == arr.length - 1 ? "" : ", ");
|
||||
}
|
||||
} else if (o instanceof double[][]) {
|
||||
double[][] arr = (double[][]) o;
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
out += printa(arr[i]) + (i == arr.length - 1 ? "" : ", ");
|
||||
}
|
||||
} else if (o instanceof boolean[][]) {
|
||||
boolean[][] arr = (boolean[][]) o;
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
out += printa(arr[i]) + (i == arr.length - 1 ? "" : ", ");
|
||||
}
|
||||
} else if (o instanceof NativeArray) {
|
||||
NativeArray arr = (NativeArray) o;
|
||||
for (long i = 0; i < arr.getLength(); i++) {
|
||||
|
||||
out += (arr.get(i) instanceof NativeArray ? printa(arr.get(i))
|
||||
: arr.get(i).toString())
|
||||
+ (i == arr.getLength() - 1 ? "" : ", ");
|
||||
}
|
||||
} else if (o instanceof PyList) {
|
||||
PyList arr = (PyList) o;
|
||||
Object[] oo = arr.toArray();
|
||||
for (int i = 0; i < oo.length; i++) {
|
||||
out += (oo[i] instanceof Object[] ? printa(oo[i]) : oo[i].toString())
|
||||
+ (i == oo.length - 1 ? "" : ", ");
|
||||
}
|
||||
} else {
|
||||
Object[] arr = (Object[]) o;
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
out += (arr[i] instanceof Object[] ? printa(arr[i]) : arr[i].toString())
|
||||
+ (i == arr.length - 1 ? "" : ", ");
|
||||
}
|
||||
}
|
||||
out += "]";
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Integrate the function with respect to idv.
|
||||
*
|
||||
@ -309,7 +423,8 @@ public class Functions {
|
||||
/**
|
||||
* Multiplies the given numbers together.
|
||||
*
|
||||
* @param a numbers. Calculates first * second * third, etc.
|
||||
* @param a
|
||||
* @param b
|
||||
* @return The product of the numbers or the value of input if there is only
|
||||
* one input.
|
||||
*/
|
||||
@ -339,7 +454,8 @@ public class Functions {
|
||||
/**
|
||||
* Divide the given numbers.
|
||||
*
|
||||
* @param a numbers. Calculates (first / second) / third, etc.
|
||||
* @param a
|
||||
* @param b
|
||||
* @return The quotient of the numbers or the value of input if there is
|
||||
* only one input.
|
||||
*/
|
||||
@ -369,7 +485,8 @@ public class Functions {
|
||||
/**
|
||||
* Divide the first number by the second and return the remainder.
|
||||
*
|
||||
* @param a numbers. Calculates (first mod second) mod third, etc.
|
||||
* @param a
|
||||
* @param b
|
||||
* @return The modulus of the numbers or the value of input if there is only
|
||||
* one input.
|
||||
*/
|
||||
@ -435,7 +552,7 @@ public class Functions {
|
||||
return ans.toString();
|
||||
}
|
||||
|
||||
public double[][] $minvert(double a[][]) {
|
||||
public double[][] minvert(double a[][]) {
|
||||
int n = a.length;
|
||||
double x[][] = new double[n][n];
|
||||
double b[][] = new double[n][n];
|
||||
@ -659,7 +776,7 @@ public class Functions {
|
||||
* @throws net.apocalypselabs.symat.BadInputException When the matrices are
|
||||
* wrong.
|
||||
*/
|
||||
public double[][] $mtimes(double[][] a, double[][] b) throws BadInputException {
|
||||
public double[][] mtimes(double[][] a, double[][] b) throws BadInputException {
|
||||
double[][] ans = new double[a.length][b[0].length];
|
||||
double sum = 0;
|
||||
int c, d, k, m = a.length, q = b[0].length, p = b.length;
|
||||
@ -690,7 +807,7 @@ public class Functions {
|
||||
* @throws BadInputException if the matrix is not square or power is less
|
||||
* than 0
|
||||
*/
|
||||
public double[][] $mpower(double[][] a, int b) throws BadInputException {
|
||||
public double[][] mpower(double[][] a, int b) throws BadInputException {
|
||||
if (a.length != a[0].length) {
|
||||
throw new BadInputException("Matrix needs to be square.");
|
||||
}
|
||||
@ -704,7 +821,7 @@ public class Functions {
|
||||
if (i == 0) {
|
||||
ans = a;
|
||||
} else {
|
||||
ans = $mtimes(a, ans);
|
||||
ans = mtimes(a, ans);
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
|
@ -1,41 +1,41 @@
|
||||
/*
|
||||
/*
|
||||
* CODE LICENSE =====================
|
||||
* Copyright (c) 2015, Apocalypse Laboratories
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation and/or
|
||||
* other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software without
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
*
|
||||
* 4. You adhere to the Media License detailed below. If you do not, this license
|
||||
* is automatically revoked and you must purge all copies of the software you
|
||||
* possess, in source or binary form.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*
|
||||
* MEDIA LICENSE ====================
|
||||
* All images and other graphical files (the "graphics") included with this
|
||||
* software are copyright (c) 2015 Apocalypse Laboratories. You may not distribute
|
||||
* the graphics or any program, source code repository, or other digital storage
|
||||
* the graphics or any program, source code repository, or other digital storage
|
||||
* media containing them without written permission from Apocalypse Laboratories.
|
||||
* This ban on distribution only applies to publicly available systems.
|
||||
* A password-protected network file share, USB drive, or other storage scheme that
|
||||
|
@ -71,4 +71,12 @@ def load(a):
|
||||
def powermod(a,b,m):
|
||||
return _.powermod(a,b,m)
|
||||
def gcd(a,b):
|
||||
return _.gcd(a,b)
|
||||
return _.gcd(a,b)
|
||||
def solve(a,b,c):
|
||||
return _.solve(a,b,c)
|
||||
def solve(a,b):
|
||||
return _.solve(a,b)
|
||||
def solve(a):
|
||||
return _.solve(a)
|
||||
def printa(a):
|
||||
return _.printa(a)
|
@ -45,4 +45,8 @@ pow(x,y)|Raise x to y and calculate.
|
||||
sin(0)|Find the sine.
|
||||
cos(0)|Find the cosine.
|
||||
tan(0)|Find the tangent.
|
||||
print("")|Prints the supplied text or formula to the output.
|
||||
print("")|Prints the supplied text or formula to the output.
|
||||
solve(f,'x',0)|Solve function f for 'x' when it equals 0.
|
||||
solve(f,'x')|Solve function f for 'x', assuming equal to 0.
|
||||
solve(f)|Solve function f, assuming 'x' and 0.
|
||||
printa(array)|Get array contents as text.
|
Loading…
x
Reference in New Issue
Block a user