Skip to content

Commit

Permalink
Added some documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
D. H. Lorenz committed Dec 26, 2023
1 parent 4525340 commit a076c37
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 13 deletions.
69 changes: 66 additions & 3 deletions function/Constant.java
Original file line number Diff line number Diff line change
@@ -1,61 +1,124 @@
/**
* This class represents a constant value used in mathematical functions.
*/
package function;

import function.arithmetics.Power;

/**
* Represents a function f(x)=1
* Represents a function f(x)=1 (a constant function that always returns the value 1).
* DO NOT USE DIRECTLY! Always surround with a LinearCombination()
*/
public class Constant extends Function {
/ public class Constant extends Function {

/**
* Creates a new Constant function with the specified scalar value.
*
* @param scalar The scalar value to multiply the Constant function by.
* @return A new Function object representing the scaled Constant function.
*/
public static Function of(double scalar) {
return FunctionVector.scale(new Constant(), scalar);
}

// to prevent usage outside of this class
/**
* Private constructor to prevent usage outside of this class.
*/
private Constant() {
}

/**
* Evaluates the Constant function at the specified value of x.
*
* @param x The value of x at which to evaluate the function.
* @return The value of the Constant function, which is always 1.
* @throws ArithmeticException if an arithmetic error occurs during evaluation.
*/
@Override
public double evaluate(double x) throws ArithmeticException {
return 1;
}

/**
* Returns the derivative of the Constant function.
*
* @return A new Function object representing the derivative of the Constant function, which is always 0.
*/
@Override
public Function derive() {
return new FunctionVector();
}

/**
* Indicates whether parentheses should be added when printing the Constant function.
*
* @return false, indicating that parentheses should not be added.
*/
@Override
public boolean shouldAddPatentheses() {
return false;
}

/**
* Substitutes the value of x in the Constant function.
*
* @param x The value to substitute in the function.
* @return An empty string, as the Constant function does not depend on the value of x.
*/
@Override
public String substitute(String x) {
return "";
}

/**
* Checks if the Constant function is equal to another object.
*
* @param other The object to compare with the Constant function.
* @return true if the other object is an instance of Constant, false otherwise.
*/
@Override
public boolean equals(Object other) {
return other instanceof Constant;
}

/**
* Multiplies the Constant function by another function.
*
* @param other The function to multiply with the Constant function.
* @return The other function, as multiplying by the Constant function does not change the value.
*/
@Override
public Function times(Function other) {
return other;
}

/**
* Divides the Constant function by another function.
*
* @param other The function to divide the Constant function by.
* @return The inverse of the other function, as dividing by the Constant function is equivalent to multiplying by its inverse.
*/
@Override
public Function div(Function other) {
return Power.inverse(other);
}

/**
* Composes the Constant function with another function.
*
* @param inner The function to compose with the Constant function.
* @return The Constant function itself, as composing with the Constant function does not change the value.
*/
@Override
public Function compose(Function inner) {
return this;
}

/**
* Returns the integral of the Constant function.
*
* @return A new Function object representing the integral of the Constant function, which is the Identity function.
*/
@Override
public Function integrate() {
return new Identity();
Expand Down
12 changes: 7 additions & 5 deletions function/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public abstract class Function {
public abstract String substitute(String x);

/**
* Should add parenthesis to a function is needed? Override in subclasses
* Should add parentheses to a function if needed? Override in subclasses
*
* @return true if should, false otherwise
* @return true if parentheses should be added, false otherwise
*/
public boolean shouldAddParentheses() {
return true;
Expand All @@ -48,8 +48,7 @@ public boolean shouldAddParentheses() {
* Show a string representation of the function
*
* @param x the value to substitute x for
* @param parenthesize is marked true if surrounding with parenthesis is
* nessecary
* @param parenthesize is marked true if surrounding with parentheses is necessary
* @return a string representation of the function
*/
public String substitute(String x, boolean parenthesize) {
Expand Down Expand Up @@ -102,7 +101,7 @@ public Function times(Function other) {
};

/**
* Divided this function by another one
* Divides this function by another one
*
* @param other the function to divide by
* @return the quotient function
Expand Down Expand Up @@ -150,6 +149,7 @@ public Function compose(Function inner) {
* another function.
*
* @param exponent the function that will be the exponent of this function
* @return the power function
*/
public Function pow(Function exponent) {
if (exponent.equals(Constant.of(1)))
Expand All @@ -170,6 +170,8 @@ public Function squared() {

/**
* Computes the integral function of a function
*
* @return the integral function
*/
public Function integrate() {
return new Integral(this);
Expand Down
52 changes: 51 additions & 1 deletion function/FunctionVector.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* This package contains classes related to ...
*/
package function;

import java.util.function.UnaryOperator;
Expand All @@ -8,7 +11,8 @@
import vector.Vector;

/**
* Rperesents a linear combination of functions.
* FunctionVector represents a vector of functions that can be combined and manipulated.
* Rperesents a linear combination of functions.
* Functions are vectors!
*
* @version 2
Expand Down Expand Up @@ -38,6 +42,15 @@ public static FunctionVector scale(Function func, double scalar) {
return vec;
}

/**
* Returns the scalar value of the given function.
* If the function is not an instance of FunctionVector, returns 1.
* If the function is a FunctionVector with size greater than 1, returns 1.
* Otherwise, returns the scalar value of the single term in the FunctionVector.
*
* @param func the function to get the scalar value from
* @return the scalar value of the function
*/
public static double getScalar(Function func) {
if (!(func instanceof FunctionVector))
return 1;
Expand Down Expand Up @@ -94,6 +107,13 @@ public FunctionVector transform(UnaryOperator<Function> transformation) {
return result;
}

/**
* Evaluates the function at a given value of x.
*
* @param x the value of x at which to evaluate the function
* @return the result of evaluating the function at x
* @throws ArithmeticException if an arithmetic error occurs during evaluation
*/
@Override
public double evaluate(double x) throws ArithmeticException {
double value = 0;
Expand All @@ -103,16 +123,32 @@ public double evaluate(double x) throws ArithmeticException {
return value;
}

/**
* Returns the derivative of the function.
*
* @return the derivative of the function
*/
@Override
public Function derive() {
return transform((term) -> term.derive());
}

/**
* Determines whether parentheses should be added when calling the function.
*
* @return true if parentheses should be added, false otherwise
*/
@Override
public boolean shouldAddPatentheses() {
return getSize() >= 1;
}

/**
* Substitutes the given value for the variable 'x' in the function and returns the result as a string.
*
* @param x the value to substitute for the variable 'x'
* @return the result of substituting the value in the function as a string
*/
@Override
public String substitute(String x) {
boolean prefixWithPlus = false;
Expand All @@ -129,6 +165,14 @@ public String substitute(String x) {
return result;
}

/**
* Substitutes a given scalar value and substitution string into a term and returns the result as a string.
*
* @param prefixWithPlus a boolean value indicating whether to prefix the result with a plus sign if the scalar is positive
* @param scalar the scalar value to substitute into the term
* @param substitution the substitution string to insert into the term
* @return the resulting term as a string
*/
private String substituteInTerm(boolean prefixWithPlus, double scalar, String substitution) {
String result = "";
double coefficient;
Expand Down Expand Up @@ -194,6 +238,12 @@ public Function div(Function other) {
return quotient;
}

/**
* Composes this Function with another Function.
*
* @param inner The inner Function to compose with.
* @return The composed Function.
*/
@Override
public Function compose(Function inner) {
// composition of vector != vector of compositions!!!
Expand Down
21 changes: 21 additions & 0 deletions function/elementary/ExponentialFunction.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* This package contains classes for elementary mathematical functions.
*/
package function.elementary;

import function.Constant;
Expand All @@ -7,18 +10,36 @@

/**
* Represents the function e to the power of f(x)
* Represents an exponential function of the form e^x.
*/
public class ExponentialFunction extends Power {

/**
* Constructs a new ExponentialFunction object.
* The base of the exponential function is the mathematical constant e (Euler's number),
* and the exponent is the identity function.
*/
public ExponentialFunction() {
super(Constant.of(Math.E), new Identity());
}

/**
* Substitutes the given value into the exponential function.
*
* @param x the value to substitute
* @return the result of substituting the value into the exponential function
*/
@Override
public String substitute(String x) {
return "e^" + x;
}

/**
* Returns the integral of the exponential function.
* Since the integral of e^x is e^x, the method returns the current exponential function itself.
*
* @return the integral of the exponential function
*/
@Override
public Function integrate() {
return this;
Expand Down
35 changes: 31 additions & 4 deletions function/trigonometric/Sine.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,63 @@
import function.FunctionVector;

/**
* Represents a sine function
* Represents a sine function.
* This class extends the TrigFunction class and provides implementation for evaluating, deriving, composing, and integrating the sine function.
*/
public class Sine extends TrigFunction {

/**
* Evaluates the sine function for the given input.
*
* @param x the input value
* @return the result of the sine function evaluation
* @throws ArithmeticException if an arithmetic error occurs
*/
@Override
public double evaluate(double x) throws ArithmeticException {
return Math.sin(x);
}

/**
* Returns the derivative of the sine function.
*
* @return the derivative of the sine function
*/
@Override
public Function derive() {
return new Cosine();
}

/**
* Returns the name of the sine function.
*
* @return the name of the sine function
*/
@Override
public String getName() {
return "sin";
}

/**
* Composes the sine function with the given inner function.
*
* @param inner the inner function to compose with
* @return the composed function
*/
@Override
public Function compose(Function inner) {

// apply identity sin(-x)=-sinx
if (FunctionVector.getScalar(inner) < 0)
return FunctionVector.scale(super.compose(FunctionVector.scale(inner, -1)),
-1);
return FunctionVector.scale(super.compose(FunctionVector.scale(inner, -1)), -1);

return super.compose(inner);
}

/**
* Returns the integral of the sine function.
*
* @return the integral of the sine function
*/
@Override
public Function integrate() {
return FunctionVector.scale(new Cosine(), -1);
Expand Down

0 comments on commit a076c37

Please sign in to comment.