forked from itays123/Function
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
356 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package function; | ||
|
||
/** | ||
* Represents the identity function f(x)=x | ||
*/ | ||
public class Identity extends Function { | ||
|
||
@Override | ||
public double evaluate(double x) throws ArithmeticException { | ||
return x; | ||
} | ||
|
||
@Override | ||
public Function derive() { | ||
return Constant.of(1); | ||
} | ||
|
||
@Override | ||
public String substitute(String x) { | ||
return x; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
return obj instanceof Identity; | ||
} | ||
|
||
@Override | ||
public Function compose(Function inner) { | ||
return inner; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package function.arithmetics; | ||
|
||
import function.Constant; | ||
import function.Function; | ||
import function.elementary.LogarithmicFunction; | ||
|
||
/** | ||
* Represents the function left^right | ||
*/ | ||
public class Power extends FunctionArithmetic { | ||
|
||
public static Function inverse(Function other) { | ||
return other.pow(Constant.of(-1)); | ||
} | ||
|
||
public Power(Function left, Function right) { | ||
super(left, right); | ||
} | ||
|
||
@Override | ||
public double evaluate(double x) throws ArithmeticException { | ||
double leftVal = left.evaluate(x); | ||
double rightVal = right.evaluate(x); | ||
if (leftVal == 0 && rightVal < 0) | ||
throw new ArithmeticException("Can't divide by 0"); | ||
return Math.pow(leftVal, rightVal); | ||
} | ||
|
||
@Override | ||
public Function derive() { | ||
// f^g is equivalent to e^(ln(f)*g) | ||
// deriving ln(f)*g gives us (f'g/f + ln(f)*g') | ||
// Therefore, the derivative would be (f'g/f + ln(f)*g') * e^(ln(f) * g), which | ||
// is equivalent to (f'g/f + ln(f)*g') * (f^g) | ||
Function innerDerivative = left.derive().times(right).div(left) | ||
.plus(right.derive().times(new LogarithmicFunction().compose(left))); | ||
return innerDerivative.times(this); | ||
} | ||
|
||
@Override | ||
public String substitute(String x) { | ||
return left.substitute(x, true) + "^" + right.substitute(x, true); | ||
} | ||
|
||
@Override | ||
public Function times(Function other) { | ||
if (other instanceof Power) { | ||
Power pow = (Power) other; | ||
|
||
// apply rule #1: a^n * a^m = a^(n+m) | ||
if (left.equals(pow.left)) | ||
return left.pow(right.plus(pow.right)); | ||
|
||
// don't apply rule #2: a^n * b^n = (ab)^n, unless simpler: | ||
if (right.equals(pow.right) && !(left.times(pow.left) instanceof Product)) | ||
return left.times(pow.left).pow(right); | ||
|
||
} | ||
|
||
// apply rule #1 again: | ||
if (left.equals(other)) | ||
return times(new Power(other, Constant.of(1))); | ||
|
||
return super.times(other); | ||
} | ||
|
||
@Override | ||
public Function div(Function other) { | ||
|
||
if (other instanceof Power) { | ||
Power pow = (Power) other; | ||
|
||
// apply rule #1: a^n / a^m = a^(n-m) | ||
if (left.equals(pow.left)) | ||
return left.pow(right.minus(pow.right)); | ||
|
||
// don't apply rule #2: a^n / b^n = (a/b)^n, unless simpler: | ||
if (right.equals(pow.right) && !(left.div(pow.left) instanceof Quotient)) | ||
return left.div(pow.left).pow(right); | ||
|
||
} | ||
|
||
// apply rule #1 again: | ||
if (left.equals(other)) | ||
return div(new Power(other, Constant.of(1))); | ||
|
||
return super.div(other); | ||
} | ||
|
||
@Override | ||
public Function compose(Function inner) { | ||
return left.compose(inner).pow(right.compose(inner)); | ||
} | ||
|
||
@Override | ||
public Function pow(Function exponent) { | ||
return left.pow(right.times(exponent)); // apply rule (a^b)^c = a^(bc) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,21 @@ | ||
package function.elementary; | ||
|
||
import function.Function; | ||
import function.FunctionVector; | ||
import function.Constant; | ||
import function.Identity; | ||
import function.arithmetics.Power; | ||
|
||
public class ExponentialFunction extends Function { | ||
|
||
private double base; | ||
/** | ||
* Represents the function e to the power of f(x) | ||
*/ | ||
public class ExponentialFunction extends Power { | ||
|
||
public ExponentialFunction() { | ||
this(Math.E); | ||
} | ||
|
||
public ExponentialFunction(double base) { | ||
if (base < 0) | ||
throw new IllegalArgumentException("Can only pick a positive base"); | ||
if (base == 1 || base == 0) | ||
throw new IllegalArgumentException("Pick a constant of " + base + " instead"); | ||
this.base = base; | ||
} | ||
|
||
@Override | ||
public double evaluate(double x) throws ArithmeticException { | ||
return Math.pow(base, x); | ||
} | ||
|
||
@Override | ||
public Function derive() { | ||
return FunctionVector.scale(new ExponentialFunction(base), Math.log(base)); | ||
super(Constant.of(Math.E), new Identity()); | ||
} | ||
|
||
@Override | ||
public String substitute(String x) { | ||
String baseStr = String.valueOf(base); | ||
if (base == Math.E) | ||
baseStr = "e"; | ||
return baseStr + "^" + x; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof ExponentialFunction)) | ||
return false; | ||
return base == ((ExponentialFunction) obj).base; | ||
return "e^" + x; | ||
} | ||
|
||
} |
Oops, something went wrong.