From 26726aea633f355327a289debbc4564aab1fd362 Mon Sep 17 00:00:00 2001 From: Skylar Date: Tue, 22 Dec 2015 02:18:13 -0700 Subject: [PATCH] Fix Java function bugs --- .../apocalypselabs/symat/CodeCompleter.java | 17 +- src/net/apocalypselabs/symat/Functions.java | 23 +- .../apocalypselabs/symat/JavaFunctions.java | 219 ++++++++++++++++++ .../apocalypselabs/symat/functions.java.txt | 4 +- .../symat/resources/functions.txt | 2 +- .../symat/resources/functions_java.txt | 63 +++++ 6 files changed, 313 insertions(+), 15 deletions(-) create mode 100644 src/net/apocalypselabs/symat/JavaFunctions.java create mode 100644 src/net/apocalypselabs/symat/resources/functions_java.txt diff --git a/src/net/apocalypselabs/symat/CodeCompleter.java b/src/net/apocalypselabs/symat/CodeCompleter.java index 51aab2e..9d938cd 100644 --- a/src/net/apocalypselabs/symat/CodeCompleter.java +++ b/src/net/apocalypselabs/symat/CodeCompleter.java @@ -94,18 +94,21 @@ public class CodeCompleter { try { String[] files = {"functions", "constants"}; for (String fileid : files) { - BufferedReader reader = new BufferedReader( - new InputStreamReader( - CodeCompleter.class.getResourceAsStream("resources/" + (fileid.equals(files[0]) ? "" : lang) + fileid + ".txt"))); + BufferedReader reader; + if (lang.equals("java")) { + reader = new BufferedReader( + new InputStreamReader( + CodeCompleter.class.getResourceAsStream("resources/" + (fileid.equals(files[0]) ? "" : lang) + fileid + (fileid.equals(files[0]) ? "_java" : "") + ".txt"))); + } else { + reader = new BufferedReader( + new InputStreamReader( + CodeCompleter.class.getResourceAsStream("resources/" + (fileid.equals(files[0]) ? "" : lang) + fileid + ".txt"))); + } String line; while ((line = reader.readLine()) != null) { switch (fileid) { case "functions": String[] args = line.split("\\|"); - // Prefix symat to Java commands - if (lang.equals("java")) { - args[0] = "symat." + args[0]; - } if (args.length == 2) { provider.addCompletion(new BasicCompletion(provider, args[0], args[1])); } else if (args.length == 3) { diff --git a/src/net/apocalypselabs/symat/Functions.java b/src/net/apocalypselabs/symat/Functions.java index 8bb5593..bd43202 100644 --- a/src/net/apocalypselabs/symat/Functions.java +++ b/src/net/apocalypselabs/symat/Functions.java @@ -711,6 +711,23 @@ public class Functions { return (new BigInteger(a).mod(new BigInteger(b))).toString(); } + public double[] intArrayToDouble(int[] a) { + double out[] = new double[a.length]; + for (int i = 0; i < a.length; i++) { + out[i] = a[i]; + } + return out; + } + + public int[] doubleArrayToInt(double[] a) { + int out[] = new int[a.length]; + for (int i = 0; i < a.length; i++) { + out[i] = Math.round((float) a[i]); + } + return out; + } + + /** * Add the given numbers together. * @@ -1043,7 +1060,7 @@ public class Functions { * @return The matrix result * @throws BadInputException if the matrix is not square or power is less * than 0 - */ + */ public double[][] mpower(double[][] a, int b) throws BadInputException { if (a.length != a[0].length) { throw new BadInputException("Matrix needs to be square."); @@ -1146,13 +1163,11 @@ public class Functions { /* Graphing interfaces */ - /** * * @param xmin * @param xmax */ - public void xlim(double xmin, double xmax) { graphwin.setRange(xmin, xmax); } @@ -1357,12 +1372,10 @@ public class Functions { /* Other */ - /** * * @return */ - public String sysinfo() { String info = "==Java Information==\n"; info += "Java version: " + System.getProperty("java.version"); diff --git a/src/net/apocalypselabs/symat/JavaFunctions.java b/src/net/apocalypselabs/symat/JavaFunctions.java new file mode 100644 index 0000000..b27a6eb --- /dev/null +++ b/src/net/apocalypselabs/symat/JavaFunctions.java @@ -0,0 +1,219 @@ +/* + * Copyright (c) 2015, Netsyms Technologies + * All rights reserved. + * + * + * CODE LICENSE ========== + * 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 + * list of conditions and the following disclaimer. + * + * 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 + * 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 + * 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 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * MEDIA LICENSE ========== + * All images and other graphical files ("media") included with this + * software are copyright (c) 2015 Netsyms Technologies. You may not distribute + * the graphics or any program, source code repository, or other digital storage + * media containing them without permission from Netsyms Technologies. + * This ban on distribution only applies to publicly available systems. + * A password-protected network file share, USB drive, or other storage scheme that + * cannot be easily accessed by the public is generally allowed. If in doubt, + * contact Netsyms Technologies. If Netsyms Technologies allows or denies + * you permission, that decision is considered final and binding. + * + * You may only use the media for personal, + * non-commercial, non-educational use unless: + * 1, You have paid for the software and media via the SyMAT website, + * or 2, you are using it as part of the 15-day trial period. + * Other uses are prohibited without permission. + * If any part of this license is deemed unenforcable, the remainder + * of the license remains in full effect. + */ +package net.apocalypselabs.symat; + +import java.util.HashSet; +import java.util.Set; + +/** + * + * @author Skylar + */ +public class JavaFunctions extends Functions { + + public JavaFunctions() { + super(); + } + + public double add(double a, double b) { + return a + b; + } + + public int add(int a, int b) { + return a + b; + } + + @Override + public double add(double[] a) { + return super.add(a); + } + + public int add(int[] a) { + return (int) super.add(intArrayToDouble(a)); + } + + public double subtract(double a, double b) { + return a - b; + } + + public int subtract(int a, int b) { + return a - b; + } + + @Override + public double subtract(double[] a) { + return super.subtract(a); + } + + public int subtract(int[] a) { + return (int) super.subtract(intArrayToDouble(a)); + } + + public double times(double a, double b) { + return a * b; + } + + public int times(int a, int b) { + return a * b; + } + + @Override + public double times(double[] a) { + return super.times(a); + } + + public int times(int[] a) { + return (int) super.times(intArrayToDouble(a)); + } + + public double divide(double a, double b) { + return a / b; + } + + public int divide(int a, int b) { + return a / b; + } + + @Override + public double divide(double[] a) { + return super.divide(a); + } + + public int divide(int[] a) { + return (int) super.divide(intArrayToDouble(a)); + } + + public void drawdot(int x, int y) { + super.drawdot(x, y); + } + + public Double[][] perms(Double... objs) { + Permutations perm = new Permutations<>(objs); + + Set perms = new HashSet<>(); + + while (perm.hasNext()) { + perms.add(perm.next()); + } + + Double[][] a = new Double[perms.size()][objs.length]; + return (Double[][]) perms.toArray(a); + } + + public Integer[][] perms(Integer... objs) { + Permutations perm = new Permutations<>(objs); + + Set perms = new HashSet<>(); + + while (perm.hasNext()) { + perms.add(perm.next()); + } + + Integer[][] a = new Integer[perms.size()][objs.length]; + return (Integer[][]) perms.toArray(a); + } + + public double[][] perms(double[] p) { + Double[] o = new Double[p.length]; + for (int i = 0; i < p.length; i++) { + o[i] = p[i]; + } + Double[][] o2 = perms(o); + double[][] out = new double[o2.length][o2[0].length]; + for (int i = 0; i < o2.length; i++) { + for (int j = 0; j < o2[0].length; j++) { + out[i][j] = o2[i][j]; + } + } + return out; + } + + public int[][] perms(int[] p) { + Integer[] o = new Integer[p.length]; + for (int i = 0; i < p.length; i++) { + o[i] = p[i]; + } + Integer[][] o2 = perms(o); + int[][] out = new int[o2.length][o2[0].length]; + for (int i = 0; i < o2.length; i++) { + for (int j = 0; j < o2[0].length; j++) { + out[i][j] = o2[i][j]; + } + } + return out; + } + + public int mod(int a, int b) { + return a % b; + } + + public double mod(double a, double b) { + return a % b; + } + + public int mod(int... a) { + return (int) super.mod(intArrayToDouble(a)); + } + + public String printa(double[][] a) { + return super.printa(a); + } + + public String printa(int[][] a) { + return super.printa(a); + } + +} diff --git a/src/net/apocalypselabs/symat/functions.java.txt b/src/net/apocalypselabs/symat/functions.java.txt index 0484c38..65e8fc1 100644 --- a/src/net/apocalypselabs/symat/functions.java.txt +++ b/src/net/apocalypselabs/symat/functions.java.txt @@ -1,5 +1,5 @@ -import net.apocalypselabs.symat.Functions; -Functions symat = new Functions(); +import net.apocalypselabs.symat.JavaFunctions; +JavaFunctions symat = new JavaFunctions(); public static final double E = java.lang.Math.E; public static final double PI = java.lang.Math.PI; public static final double SQRT1_2 = java.lang.Math.sqrt(0.5); diff --git a/src/net/apocalypselabs/symat/resources/functions.txt b/src/net/apocalypselabs/symat/resources/functions.txt index 358d217..5ad42a5 100644 --- a/src/net/apocalypselabs/symat/resources/functions.txt +++ b/src/net/apocalypselabs/symat/resources/functions.txt @@ -4,7 +4,7 @@ diff('',"x")|Find the derivative of the function passed with respect to the seco integrate('',"x")|Find the integral of the function passed with respect to the second argument. rad(0)|Convert a given number in degrees to radians. deg(0)|Convert a given number in radians to degrees. -subs('',"x")|Solve an equation for the second argument. +subs("","x","0")|Solve an equation by replacing the second argument with the third. simplify('')|Simplify the given function. vpa('')|Computes numerical value or simplifies. plot()|Show the plot window. diff --git a/src/net/apocalypselabs/symat/resources/functions_java.txt b/src/net/apocalypselabs/symat/resources/functions_java.txt new file mode 100644 index 0000000..738ca6c --- /dev/null +++ b/src/net/apocalypselabs/symat/resources/functions_java.txt @@ -0,0 +1,63 @@ +symat.notify("")|Display a message in a box. +symat.ask("")|Ask a question in a box. Supply question text. +symat.diff('',"x")|Find the derivative of the function passed with respect to the second argument. +symat.integrate('',"x")|Find the integral of the function passed with respect to the second argument. +symat.rad(0)|Convert a given number in degrees to radians. +symat.deg(0)|Convert a given number in radians to degrees. +symat.subs("","x","0")|Solve an equation by replacing the second argument with the third. +symat.simplify('')|Simplify the given function. +symat.vpa('')|Computes numerical value or simplifies. +symat.plot()|Show the plot window. +symat.plot('')|Graph the given function. +symat.plotname("")|Sets the title of the graph window. +symat.plotname()|Gets the title of the graph window. +symat.xlim(min,max)|Sets the x-axis min and max values. Cannot be used after a formula has been graphed. +symat.plotclr()|Reset the graph. +symat.drawdot(x, y)|Places a dot at the given coordinates. +symat.readfile("")|Read a text file from the given filename. +symat.savefile(data,"")|Save the text data to a file. +symat.save(key,val)|Save the text val to text key. +symat.load(key)|Load the saved text related to key. +symat.rand()|Random fraction from 0 (inclusive) to 1 (exclusive) +symat.rand(min,max)|Random number between min and max, inclusive +symat.randb()|Random boolean (true or false) +symat.perms(items)|Array of all possible combinations. +symat.add(n1,n2,...)|Add the given numbers. +symat.subtract(n1,n2,...)|Subtract the given numbers. +symat.times(n1,n2,...)|Multiply the given numbers. +symat.divide(n1,n2,...)|Divide the given numbers. +symat.mod(n1,n2,...)|Divide the numbers and return the remainder. +symat.factorial(n)|Get the factorial (n!) of n as a string. +symat.factor(n)|Get an array of all the factors of n. +Math.abs(0)|Absolute value of number. +Math.asin(0)|Arcsine of number. +Math.acos(0)|Arccosine of number. +Math.atan(0)|Arctangent of number. +Math.ceil(0)|Round the number up to the next integer. +Math.exp(0)|e^x. +symat.gcd(a,b)|Find greatest common divisor of a and b. +Math.floor(0)|Round the number down to the next integer. +Math.log(0)|Natural log of number. +Math.max(0,1,...)|Returns the highest number given. +Math.min(0,1,...)|Returns the lowest number given. +symat.powermod(a,b,m)|Find pow(a,b) mod m, or modular inverse if b=-1. +Math.pow(x,y)|Raise x to y and calculate. +Math.sin(0)|Find the sine. +Math.cos(0)|Find the cosine. +Math.tan(0)|Find the tangent. +symat.sinh(0)|Find the hyperbolic sine. +symat.cosh(0)|Find the hyperbolic cosine. +symat.tanh(0)|Find the hyperbolic tangent. +symat.sec(0)|Find the secant. +symat.csc(0)|Find the cosecant. +symat.cot(0)|Find the cotangent. +symat.sech(0)|Find the hyperbolic secant. +symat.csch(0)|Find the hyperbolic cosecant. +symat.coth(0)|Find the hyperbolic cotangent. +print("")|Prints the supplied text or formula to the output. +symat.solve(f,'x',0)|Solve function f for 'x' when it equals 0. +symat.solve(f,'x')|Solve function f for 'x', assuming equal to 0. +symat.solve(f)|Solve function f, assuming 'x' and 0. +symat.printa(array)|Get array contents as text. +symat.primes(n)|Find all prime numbers up to n. +symat.filedialog()|Open a file chooser and return the chosen file path. \ No newline at end of file