Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
PB2204 committed Oct 6, 2023
2 parents 1c4c2d0 + 19114ce commit 8380205
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 5 deletions.
42 changes: 42 additions & 0 deletions adv-math/src/equation-solvers/equations.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,48 @@ class Equations {
const x2 = (-b - sqrtDiscriminant) / (2 * a);
return [x1, x2];
}

// Method to solve a cubic equation of the form: ax^3 + bx^2 + cx + d = 0
static solveCubic(a, b, c, d) {
// Implementation of Cardano's method to solve cubic equations
if (a === 0) {
throw new Error('The coefficient of x^3 cannot be zero.');
}

const delta0 = b * b - 3 * a * c;
const delta1 = 2 * b * b * b - 9 * a * b * c + 27 * a * a * d;
const C = Math.cbrt((delta1 + Math.sqrt(delta1 * delta1 - 4 * delta0 * delta0 * delta0)) / 2);
const x1 = -(1 / (3 * a)) * (b + C + delta0 / C);
return [x1];
}

// Method to solve an exponential equation of the form: a^x = b
static solveExponential(a, b) {
if (a <= 0 || a === 1 || b <= 0) {
throw new Error('Values of a and b must be positive, and a cannot be equal to 1.');
}
return Math.log(b) / Math.log(a);
}

// Method to solve a logarithmic equation of the form: log_a(bx + c) = d
static solveLogarithmic(a, b, c, d) {
if (a <= 0 || a === 1 || b <= 0 || c < 0) {
throw new Error('Values of a, b, and c must be positive, and a cannot be equal to 1.');
}
const logValue = d / Math.log(a);
return (Math.pow(a, logValue) - c) / b;
}

// Method to solve a system of linear equations
static solveLinearSystem(a1, b1, c1, a2, b2, c2) {
const determinant = a1 * b2 - a2 * b1;
if (determinant === 0) {
throw new Error('The system of equations has no unique solution.');
}
const x = (c1 * b2 - c2 * b1) / determinant;
const y = (a1 * c2 - a2 * c1) / determinant;
return [x, y];
}
}

module.exports = Equations;
69 changes: 67 additions & 2 deletions adv-math/src/financial/financial.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class Financial {
// Method to calculate the future value of an investment with compound interest
static futureValue(principal, rate, time) {
const compoundInterest = principal * Math.pow(1 + rate, time);
return compoundInterest;
return principal * Math.pow(1 + rate, time);
}

// Method to calculate the compound interest earned on an investment
Expand All @@ -14,6 +13,72 @@ class Financial {
static presentValue(futureValue, rate, time) {
return futureValue / Math.pow(1 + rate, time);
}

// Method to calculate the loan amortization schedule
static loanAmortization(principal, rate, time) {
const monthlyRate = rate / 12 / 100; // Monthly interest rate
const numPayments = time * 12; // Number of monthly payments
const monthlyPayment = (principal * monthlyRate) / (1 - Math.pow(1 + monthlyRate, -numPayments));

const amortizationSchedule = [];
let remainingBalance = principal;

for (let month = 1; month <= numPayments; month++) {
const interestPayment = remainingBalance * monthlyRate;
const principalPayment = monthlyPayment - interestPayment;
remainingBalance -= principalPayment;

amortizationSchedule.push({
month,
monthlyPayment,
principalPayment,
interestPayment,
remainingBalance,
});
}

return amortizationSchedule;
}

// Method to calculate the effective interest rate
static effectiveInterestRate(principal, futureValue, time) {
return Math.pow(futureValue / principal, 1 / time) - 1;
}

// Method to convert annual interest rates to monthly
static annualToMonthlyInterestRate(annualRate) {
return (Math.pow(1 + annualRate, 1 / 12) - 1) * 100;
}

// Method to calculate the net present value (NPV) of cash flows
static netPresentValue(cashFlows, discountRate) {
let npv = 0;
for (let i = 0; i < cashFlows.length; i++) {
npv += cashFlows[i] / Math.pow(1 + discountRate, i);
}
return npv;
}

// Method to adjust a value for inflation
static adjustForInflation(value, inflationRate, years) {
return value / Math.pow(1 + inflationRate, years);
}

// Method to calculate the periodic payment needed to reach a future value goal
static calculateRequiredPaymentForFutureValue(futureValue, rate, time) {
return futureValue / ((Math.pow(1 + rate, time) - 1) / rate);
}

// Method to calculate asset depreciation
static calculateDepreciation(initialValue, salvageValue, usefulLife) {
return (initialValue - salvageValue) / usefulLife;
}

// Method to calculate the total return on an investment
static totalReturn(initialInvestment, finalValue, additionalInvestments) {
return (finalValue - initialInvestment + additionalInvestments) / initialInvestment;
}
}

module.exports = Financial;

68 changes: 68 additions & 0 deletions adv-math/src/geometry-trigonometry/geometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,74 @@ class Geometry {
static circleCircumference(radius) {
return 2 * Math.PI * radius;
}

// Method to calculate the area of a triangle given base and height
static triangleArea(base, height) {
return (base * height) / 2;
}

// Method to calculate the volume of a sphere given its radius
static sphereVolume(radius) {
return (4 / 3) * Math.PI * Math.pow(radius, 3);
}

// Method to calculate the area of an equilateral triangle given its side length
static equilateralTriangleArea(side) {
return (Math.sqrt(3) / 4) * Math.pow(side, 2);
}
//Method to calulate the area of the triangle given its side lengths
static triangleArea_sides(side1, side2, side3) {
const s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
//Method to calulate the area of a square given its side length
static squareArea(side) {
return Math.pow(side, 2);
}
//Method to calculate the perimeter of a square given its side length
static squarePerimeter(side) {
return 4 * side;
}

// Method to calculate the volume of a cube given its side length
static cubeVolume(side) {
return Math.pow(side, 3);
}

// Method to calculate the volume of a rectangular prism given length, width, and height
static rectangularPrismVolume(length, width, height) {
return length * width * height;
}

// Method to calculate the surface area of a rectangular prism given length, width, and height
static rectangularPrismSurfaceArea(length, width, height) {
return 2 * (length * width + width * height + height * length);
}

// Method to calculate the volume of a cylinder given its radius and height
static cylinderVolume(radius, height) {
return Math.PI * Math.pow(radius, 2) * height;
}

// Method to calculate the surface area of a cylinder given its radius and height
static cylinderSurfaceArea(radius, height) {
const baseArea = Math.PI * Math.pow(radius, 2);
const lateralArea = 2 * Math.PI * radius * height;
return 2 * baseArea + lateralArea;
}

// Method to calculate the volume of a cone given its radius and height
static coneVolume(radius, height) {
return (1 / 3) * Math.PI * Math.pow(radius, 2) * height;
}

// Method to calculate the surface area of a cone given its radius and height
static coneSurfaceArea(radius, height) {
const slantHeight = Math.sqrt(Math.pow(radius, 2) + Math.pow(height, 2));
const baseArea = Math.PI * Math.pow(radius, 2);
const lateralArea = Math.PI * radius * slantHeight;
return baseArea + lateralArea;
}
}

module.exports = Geometry;
42 changes: 42 additions & 0 deletions adv-math/src/geometry-trigonometry/trigonometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,48 @@ class Trigonometry {
}
return this.arctangent(1 / value);
}

// Method to calculate the hyperbolic sine of an angle in degrees
static sinh(degrees) {
const radians = (degrees * Math.PI) / 180;
return Math.sinh(radians);
}

// Method to calculate the hyperbolic cosine of an angle in degrees
static cosh(degrees) {
const radians = (degrees * Math.PI) / 180;
return Math.cosh(radians);
}

// Method to calculate the hyperbolic tangent of an angle in degrees
static tanh(degrees) {
const radians = (degrees * Math.PI) / 180;
return Math.tanh(radians);
}

// Method to calculate the hyperbolic arcsine in degrees
static arsinh(value) {
const result = Math.asinh(value);
return (result * 180) / Math.PI;
}

// Method to calculate the hyperbolic arccosine in degrees
static arcosh(value) {
if (value < 1) {
throw new Error('Invalid input for arcosh.');
}
const result = Math.acosh(value);
return (result * 180) / Math.PI;
}

// Method to calculate the hyperbolic arctangent in degrees
static artanh(value) {
if (value < -1 || value > 1) {
throw new Error('Invalid input for artanh.');
}
const result = Math.atanh(value);
return (result * 180) / Math.PI;
}
}

module.exports = Trigonometry;
12 changes: 11 additions & 1 deletion adv-math/src/units-conversions/units.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,19 @@ class Units {
static convertTemperature(value, fromUnit, toUnit) {
if (fromUnit === 'Celsius' && toUnit === 'Fahrenheit') {
return (value * 9 / 5) + 32;
} else if (fromUnit=='Celsius' && toUnit=='Kelvin') {
return value + 273.15;
} else if (fromUnit === 'Fahrenheit' && toUnit === 'Celsius') {
return (value - 32) * 5 / 9;
} else {
} else if (fromUnit=='Fahrenheit' && toUnit=='Kelvin') {
return (value + 459.67) * 5 / 9;
} else if (fromUnit=='Kelvin' && toUnit=='Celsius') {
return value - 273.15;
} else if (fromUnit=='Kelvin' && toUnit=='Fahrenheit') {
return (value * 9 / 5) - 459.67;
}

else {
throw new Error('Invalid temperature unit conversion.');
}
}
Expand Down
40 changes: 39 additions & 1 deletion adv-math/tests/geometry-trigonometry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,45 @@ describe('Geometry', () => {
test('calculates the circumference of a circle', () => {
expect(Geometry.circleCircumference(3)).toBeCloseTo(18.850, 3);
});

test('calculates the area of a triangle', () => {
expect(Geometry.triangleArea(3, 4)).toBe(6);
});
test('calculates the volume of a sphere', () => {
expect(Geometry.sphereVolume(3)).toBeCloseTo(113.097, 3);
});
test('calculates the area of an equilateral triangle', () => {
expect(Geometry.equilateralTriangleArea(3)).toBeCloseTo(3.897, 3);
});
test('calculates the area of a triangle given its side lengths', () => {
expect(Geometry.triangleArea_sides(3, 4, 5)).toBe(6);
});
test('calculates the area of a square given its side length', () => {
expect(Geometry.squareArea(3)).toBe(9);
});
test('calculates the perimeter of a square given its side length', () => {
expect(Geometry.squarePerimeter(3)).toBe(12);
});
test('calculates the volume of a cube given its side length', () => {
expect(Geometry.cubeVolume(3)).toBe(27);
});
test('calculates the volume of a rectangular prism given length, width, and height', () => {
expect(Geometry.rectangularPrismVolume(3, 4, 5)).toBe(60);
});
test('calculates the surface area of a rectangular prism given length, width, and height', () => {
expect(Geometry.rectangularPrismSurfaceArea(3, 4, 5)).toBe(94);
});
test('calculates the volume of a cylinder given radius and height', () => {
expect(Geometry.cylinderVolume(3, 5)).toBeCloseTo(141.371, 3);
});
test('calculates the surface area of a cylinder given radius and height', () => {
expect(Geometry.cylinderSurfaceArea(3, 5)).toBeCloseTo(150.796, 3);
});
test('calculates the volume of a cone given radius and height', () => {
expect(Geometry.coneVolume(3, 5)).toBeCloseTo(47.123, 3);
});
test('calculates the surface area of a cone given radius and height', () => {
expect(Geometry.coneSurfaceArea(3, 5)).toBeCloseTo(83.229, 3);
});
// Add more test cases as needed
});

Expand Down
13 changes: 12 additions & 1 deletion adv-math/tests/units-conversions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,21 @@ describe('Units', () => {
test('converts Celsius to Fahrenheit', () => {
expect(Units.convertTemperature(25, 'Celsius', 'Fahrenheit')).toBeCloseTo(77, 1);
});

test('converts Celsius to Kelvin', () => {
expect(Units.convertTemperature(25, 'Celsius', 'Kelvin')).toBeCloseTo(298.15, 1);
});
test('converts Fahrenheit to Kelvin', () => {
expect(Units.convertTemperature(68, 'Fahrenheit', 'Kelvin')).toBeCloseTo(293.15, 1);
});
test('converts Fahrenheit to Celsius', () => {
expect(Units.convertTemperature(68, 'Fahrenheit', 'Celsius')).toBeCloseTo(20, 1);
});
test('converts Kelvin to Celsius', () => {
expect(Units.convertTemperature(300, 'Kelvin', 'Celsius')).toBeCloseTo(26.85, 1);
});
test('converts Kelvin to Fahrenheit', () => {
expect(Units.convertTemperature(300, 'Kelvin', 'Fahrenheit')).toBeCloseTo(80.33, 1);
});

// Add more test cases as needed
});

0 comments on commit 8380205

Please sign in to comment.