Complex Calculator
Complex Calculator is a java-based project helping to evaluate arithmetical operations
Getting Started
Version 3.0.0
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
Prerequisites
You need to have a maven or gradle project. Preferably in an IDE such as IntelliJ, Eclipse or NetBeans.
Installing
If you are using gradle, this is what you must do,
repositories {
maven {
url 'https://jitpack.io'
}
}
dependencies {
implementation 'com.github.cybercoder-naj:ComplexCalculator:3.0.0'
}
If you are using maven, this is what you must do,
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.cybercoder-naj</groupId>
<artifactId>ComplexCalculator</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
How to Use
After adding the dependencies, you must import the class.
import com.nishant.complexcalculator.ComplexCalulator;
- Visit ComplexCalculator to see how to use that class.
- Visit DifferentialCalculator to see how to use that class.
Using Constant Functions
You need create an object of the class and pass in a string which holds the constant function.
String function = "3+4^2/8";
ComplexCalculator calc = ComplexCalculator.fromString(function);
Calling the compute function will return the value of the given expression.
System.out.println(calc.compute());
The output of the following is 5.0.
Using Dependent Functions
You need create an object of the class and pass in a string which holds the dependent function. Create a Map<Character, Double> Collection and put the variables as key and the value it holds as value.
String function = "(x^2 - 4)/(x - 2)";
ComplexCalculator calc = ComplexCalculator.fromString(function);
Map<Character, Double> variableMap = new HashMap<>();
variableMap.put('x', 4.0);
Calling the compute function will return the value of the given expression.
System.out.println(calc.compute(variableMap));
The output of the following is 6.0.
Using Exponents
You can use exponents in the given string expression following the rules of using exponents in double data type. The rules for using exponents are:-
- The digits after E must not be fractional.
- The digits before E must have a integral and a fractional part.
String function = "6.626E-34/(m*v)";
ComplexCalculator calc = ComplexCalculator.fromString(function);
Map<Character, Double> variableMap = new HashMap<>();
variableMap.put('m', 9.1E-31);
variableMap.put('v', 2.1E6);
System.out.println(calc.compute(variableMap));
The output of the following is 3.467294610151753E-10.
Using Pi(π) and e
You can use pi in the middle of the expression to use its value.
String function = "pi*r^2 + e^x";
ComplexCalculator calc = ComplexCalculator.fromString(function);
Map<Character, Double> variableMap = new HashMap<>();
variableMap.put('r', 7.0);
variableMap.put('x', 0.5);
System.out.println(calc.compute(variableMap));
The output of the following is 155.58676129659997.
Using Trigonometric Functions
You can use trigonometric functions in the the expression. For traditional trigonometric functions, use the short-hand notation.
- sine (sin)
- cosine (cos)
- tangent (tan)
- cotangent (cot)
- secant (sec)
- cosecant (csc)
String function = "sin(pi/6)^2 + cos(pi/6)^2";
ComplexCalculator calc = ComplexCalculator.fromString(function);
System.out.println(calc.compute());
The output of the following is 1.0.
Using Inverse Trigonometric Functions
To use trigonometric functions, you have to prefix "arc" to the short-hand trigonometric function notation given here.
String function = "arctan(1)";
ComplexCalculator calc = ComplexCalculator.fromString(function);
System.out.println(calc.compute() * 180.0 / Math.PI);
The output of the following is 45.0.
Using Logarithmic Function
To calculate the natural logarithm of a number, use ln in the function. Note: the domain of the function is 0 < x < infinity.
String function = "ln(sin(pi/2))";
ComplexCalculator calc = ComplexCalculator.fromString(function);
System.out.println(calc.compute());
The output of the following is 0.0.
Using Absolute Function
The absolute function gives the positive value of the number. For example |x|.
String function = "|ln(1/e)|";
ComplexCalculator calc = ComplexCalculator.fromString(function);
System.out.println(calc.compute());
The output of the following is 1.0.
Finding Derivative at a point
You need create an object of the class and pass in a string which holds the function. The function should only use one variable x.
String function = "x^2 - 5*x + 6";
DifferentialCalculator calc = DifferentialCalculator.fromString(function);
double answer = calc.differentiateAt(2.0);
System.out.println(Math.round(answer * 1000) / 1000.0);
The output of the following is -1.0.
Finding the area under the curve in a range.
You need to create a object of the class and pass in a string which holds the function. The function should only use one variable x.
String function = "e^x + 1";
DifferentialCalculator calc = DifferentialCalculator.fromString(function);
double answer = calc.integrate(-10, 10);
System.out.println(Math.round(answer * 1000) / 1000.0);
The output of the following is 22037.657
Project Documentation
Don't forget to download the documentation. Visit Documentation to view the whole documentation.
License
This project is licensed under Apache License Version 2.0 - see License