diff options
| -rw-r--r-- | CSMath/.classpath | 26 | ||||
| -rw-r--r-- | CSMath/.gitignore | 1 | ||||
| -rw-r--r-- | CSMath/.project | 6 | ||||
| -rw-r--r-- | CSMath/.settings/org.eclipse.jdt.core.prefs | 1 | ||||
| -rw-r--r-- | CSMath/pom.xml | 28 | ||||
| -rw-r--r-- | CSMath/src/bisection/Bisection.java | 3 | ||||
| -rw-r--r-- | CSMath/src/bisection/Dual.java | 55 | ||||
| -rw-r--r-- | CSMath/src/bisection/DualExpr.java | 270 | ||||
| -rw-r--r-- | CSMath/src/bisection/NeoBisection.java | 3 |
9 files changed, 62 insertions, 331 deletions
diff --git a/CSMath/.classpath b/CSMath/.classpath index e461bea..ef141e6 100644 --- a/CSMath/.classpath +++ b/CSMath/.classpath @@ -1,6 +1,20 @@ -<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
- <classpathentry kind="src" path="src"/>
- <classpathentry kind="output" path="bin"/>
-</classpath>
+<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> + <attributes> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="src" output="target/classes" path="src"> + <attributes> + <attribute name="optional" value="true"/> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> + <attributes> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="output" path="target/classes"/> +</classpath> diff --git a/CSMath/.gitignore b/CSMath/.gitignore index ae3c172..09e3bc9 100644 --- a/CSMath/.gitignore +++ b/CSMath/.gitignore @@ -1 +1,2 @@ /bin/ +/target/ diff --git a/CSMath/.project b/CSMath/.project index 453ea31..ab16557 100644 --- a/CSMath/.project +++ b/CSMath/.project @@ -10,8 +10,14 @@ <arguments>
</arguments>
</buildCommand>
+ <buildCommand>
+ <name>org.eclipse.m2e.core.maven2Builder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
</buildSpec>
<natures>
+ <nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
diff --git a/CSMath/.settings/org.eclipse.jdt.core.prefs b/CSMath/.settings/org.eclipse.jdt.core.prefs index bb35fa0..df46a9a 100644 --- a/CSMath/.settings/org.eclipse.jdt.core.prefs +++ b/CSMath/.settings/org.eclipse.jdt.core.prefs @@ -8,4 +8,5 @@ org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
diff --git a/CSMath/pom.xml b/CSMath/pom.xml new file mode 100644 index 0000000..7c1dbd7 --- /dev/null +++ b/CSMath/pom.xml @@ -0,0 +1,28 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>bjc</groupId> + <artifactId>CSMath</artifactId> + <version>0.0.1-SNAPSHOT</version> + <name>CSMath</name> + <description>Projects from Advanced CS Math class</description> + <build> + <sourceDirectory>src</sourceDirectory> + <plugins> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.7.0</version> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + </plugins> + </build> + <dependencies> + <dependency> + <groupId>bjc</groupId> + <artifactId>BJC-Utils2</artifactId> + <version>1.0.0</version> + </dependency> + </dependencies> +</project>
\ No newline at end of file diff --git a/CSMath/src/bisection/Bisection.java b/CSMath/src/bisection/Bisection.java index 324d127..039c9b9 100644 --- a/CSMath/src/bisection/Bisection.java +++ b/CSMath/src/bisection/Bisection.java @@ -8,6 +8,9 @@ package bisection; * Use the bisection method to find bracketed roots of arbitrary real-valued functions.
*/
+import bjc.utils.math.Dual;
+import bjc.utils.math.DualExpr;
+
/**
* Bisect a curve to find bracketed roots of arbitrary real-valued functions.
* @author bjculkin
diff --git a/CSMath/src/bisection/Dual.java b/CSMath/src/bisection/Dual.java deleted file mode 100644 index de5b004..0000000 --- a/CSMath/src/bisection/Dual.java +++ /dev/null @@ -1,55 +0,0 @@ -package bisection;
-
-/**
- * Represents a 'dual' number.
- *
- * Think imaginary numbers, where instead of i, we add a value d such that d^2 =
- * 0.
- */
-public class Dual {
- /**
- * The real part of the dual number.
- */
- public double real;
- /**
- * The dual part of the dual number.
- */
- public double dual;
-
- /**
- * Create a new dual with both parts zero.
- */
- public Dual() {
- real = 0;
- dual = 0;
- }
-
- /**
- * Create a new dual number with a zero dual part.
- *
- * @param real
- * The real part of the number.
- */
- public Dual(double real) {
- this.real = real;
- this.dual = 0;
- }
-
- /**
- * Create a new dual number with a specified dual part.
- *
- * @param real
- * The real part of the number.
- * @param dual
- * The dual part of the number.
- */
- public Dual(double real, double dual) {
- this.real = real;
- this.dual = dual;
- }
-
- @Override
- public String toString() {
- return String.format("<%f, %f>", real, dual);
- }
-}
\ No newline at end of file diff --git a/CSMath/src/bisection/DualExpr.java b/CSMath/src/bisection/DualExpr.java deleted file mode 100644 index 0c76840..0000000 --- a/CSMath/src/bisection/DualExpr.java +++ /dev/null @@ -1,270 +0,0 @@ -package bisection;
-
-/**
- * Represents an expression using dual numbers.
- *
- * Useful for automatically differentiating expressions.
- */
-public class DualExpr {
- /**
- * Represents the various types of dual expressions.
- */
- public static enum ExprType {
- /**
- * A fixed number.
- */
- CONSTANT,
- /**
- * An addition operation.
- */
- ADDITION,
- /**
- * A subtraction operation.
- */
- SUBTRACTION,
- /**
- * A multiplication operation.
- */
- MULTIPLICATION,
- /**
- * A division operation.
- */
- DIVISION,
- /**
- * A sine operation.
- */
- SIN,
- /**
- * A cosine operation.
- */
- COS,
- /**
- * An exponential function.
- */
- EXPONENTIAL,
- /**
- * A logarithm function.
- */
- LOGARITHM,
- /**
- * A power operation.
- */
- POWER,
- /**
- * An absolute value.
- */
- ABSOLUTE
- }
-
- /**
- * The type of the expression.
- */
- public final DualExpr.ExprType type;
-
- /**
- * The dual number value, for constants.
- */
- public Dual number;
-
- /**
- * The left (or first) part of the expression.
- */
- public DualExpr left;
- /**
- * The right (or second) part of the expression.
- */
- public DualExpr right;
-
- /**
- * The power to use, for power operations.
- */
- public int power;
-
- /**
- * Create a new constant dual number.
- *
- * @param num
- * The value of the dual number.
- */
- public DualExpr(Dual num) {
- this.type = ExprType.CONSTANT;
-
- number = num;
- }
-
- /**
- * Create a new unary dual number.
- *
- * @param type
- * The type of operation to perform.
- * @param val
- * The parameter to the value.
- */
- public DualExpr(DualExpr.ExprType type, DualExpr val) {
- this.type = type;
-
- left = val;
- }
-
- /**
- * Create a new math expression.
- * - * The type of operation to perform.
- * @param left
- * The left operand
- * @param right
- * The right operand - */
- public DualExpr(DualExpr.ExprType type, DualExpr left, DualExpr right) {
- this.type = type;
-
- this.left = left;
- this.right = right;
- }
-
- /**
- * Create a new power expression.
- *
- * @param left
- * The expression to raise.
- * @param power
- * The power to raise it by.
- */
- public DualExpr(DualExpr left, int power) {
- this.type = ExprType.POWER;
-
- this.left = left;
- this.power = power;
- }
-
- /**
- * Evaluate an expression to a number.
- *
- * Uses the rules provided in
- * https://en.wikipedia.org/wiki/Automatic_differentiation
- *
- * @return The evaluated expression.
- */
- public Dual evaluate() {
- /* The evaluated dual numbers. */
- Dual lval, rval;
-
- /* Perform the right operation for each type. */
- switch (type) {
- case CONSTANT:
- return number;
- case ADDITION:
- lval = left.evaluate();
- rval = right.evaluate();
-
- return new Dual(lval.real + rval.real, lval.dual + rval.dual);
- case SUBTRACTION:
- lval = left.evaluate();
- rval = right.evaluate();
-
- return new Dual(lval.real - rval.real, lval.dual - rval.dual);
- case MULTIPLICATION:
- lval = left.evaluate();
- rval = right.evaluate();
-
- {
- double lft = lval.dual * rval.real;
- double rght = lval.real * rval.dual;
-
- return new Dual(lval.real * rval.real, lft + rght);
- }
- case DIVISION:
- lval = left.evaluate();
- rval = right.evaluate();
-
- {
- if (rval.real == 0) {
- throw new IllegalArgumentException("ERROR: Attempted to divide by zero.");
- }
-
- double lft = lval.dual * rval.real;
- double rght = lval.real * rval.dual;
-
- double val = (lft - rght) / (rval.real * rval.real);
-
- return new Dual(lval.real / rval.real, val);
- }
- case SIN:
- lval = left.evaluate();
-
- return new Dual(Math.sin(lval.real), lval.dual * Math.cos(lval.real));
- case COS:
- lval = left.evaluate();
-
- return new Dual(Math.cos(lval.real), -lval.dual * Math.sin(lval.real));
- case EXPONENTIAL:
- lval = left.evaluate();
-
- {
- double val = Math.exp(lval.real);
-
- return new Dual(val, lval.dual * val);
- }
- case LOGARITHM:
- lval = left.evaluate();
-
- if (lval.real <= 0) {
- throw new IllegalArgumentException("ERROR: Attempted to take non-positive log.");
- }
-
- return new Dual(Math.log(lval.real), lval.dual / lval.real);
- case POWER:
- lval = left.evaluate();
-
- if (lval.real == 0) {
- throw new IllegalArgumentException("ERROR: Raising zero to a power.");
- }
-
- {
- double rl = Math.pow(lval.real, power);
-
- double lft = Math.pow(lval.real, power - 1);
-
- return new Dual(rl, power * lft * lval.dual);
- }
- case ABSOLUTE:
- lval = left.evaluate();
-
- return new Dual(Math.abs(lval.real), lval.dual * Math.signum(lval.real));
- default:
- String msg = "ERROR: Unknown expression type %s";
-
- throw new IllegalArgumentException(String.format(msg, type));
- }
- }
-
- @Override
- public String toString() {
- switch (type) {
- case ABSOLUTE:
- return String.format("abs(%s)", left.toString());
- case ADDITION:
- return String.format("(%s + %s)", left.toString(), right.toString());
- case CONSTANT:
- return String.format("%s", number.toString());
- case COS:
- return String.format("cos(%s)", left.toString());
- case DIVISION:
- return String.format("(%s / %s)", left.toString(), right.toString());
- case EXPONENTIAL:
- return String.format("exp(%s)", left.toString());
- case LOGARITHM:
- return String.format("log(%s)", left.toString());
- case MULTIPLICATION:
- return String.format("(%s * %s)", left.toString(), right.toString());
- case POWER:
- return String.format("(%s ^ %d)", left.toString(), power);
- case SIN:
- return String.format("sin(%s)", left.toString());
- case SUBTRACTION:
- return String.format("(%s - %s)", left.toString(), right.toString());
- default:
- return String.format("UNKNOWN_EXPR");
- }
- }
-}
\ No newline at end of file diff --git a/CSMath/src/bisection/NeoBisection.java b/CSMath/src/bisection/NeoBisection.java index fddb900..530fbeb 100644 --- a/CSMath/src/bisection/NeoBisection.java +++ b/CSMath/src/bisection/NeoBisection.java @@ -2,6 +2,9 @@ package bisection; import java.util.function.DoubleUnaryOperator;
+import bjc.utils.math.Dual;
+import bjc.utils.math.DualExpr;
+
/*
* Benjamin Culkin
* 1/16/2018
|
