math-problems
Version:
Solve math problems easily with this utility library.
270 lines (262 loc) • 7.38 kB
JavaScript
// src/physics/temperatures.ts
function toCelsius(temperature, unit) {
if (unit === "C") return temperature;
if (unit === "K") return Math.floor(temperature - 273.15);
return Math.max((temperature - 32) * 5 / 9);
}
function toFahrenheit(temperature, unit) {
if (unit === "F") return temperature;
if (unit === "K") return Math.floor(temperature * 5 / 9 + 459.67);
return Math.max(temperature * 9 / 5 + 32);
}
function toKelvin(temperature, unit) {
if (unit === "K") return temperature;
if (unit === "F") return Math.floor((temperature + 459.67) * 5 / 9);
return Math.max(temperature + 273.15);
}
// src/physics/physics.ts
var force = (mass, acceleration) => mass * acceleration;
var kineticEnergy = (mass, velocity) => 0.5 * mass * velocity ** 2;
var potentialEnergy = (mass, height, g = 9.81) => mass * g * height;
var momentum = (mass, velocity) => mass * velocity;
// src/math/numbers.ts
var PI = Math.PI;
function isEven(number) {
return number % 2 == 0;
}
function isOdd(number) {
return !isEven(number);
}
function difference(x, y) {
return Math.max(x - y);
}
function squared(x, times) {
return Math.pow(x, times ?? 2);
}
function hypotenuse(C, c) {
var h = squared(C) + squared(c);
return Math.sqrt(h);
}
function cathetus(H, C) {
if (H <= C)
throw new Error("Cathetus cannot be greater or equal than Hypotenuse.");
return Math.sqrt(squared(H) - squared(C));
}
// src/math/area.ts
var area = {
/**
* @param {number} base Size of the base of the rectangle
* @param {number} height The height of the rectangle
*
* @returns {number} base * height
*/
rect: (base, height) => {
return Math.floor(base * height);
},
/**
* @param {number} base Size of the base of the triangle
* @param {number} height The height of the triangle
*
* @returns {number} (base * height) / 2
*/
triangle: (base, height) => {
return Math.floor(base * height / 2);
},
/**
* @param {number} D larger diagonal
* @param {number} d smaller diagonal
*
* @returns {number} (D * d) / 2
*/
rhombus: (D, d) => {
return Math.floor(D * d / 2);
},
/**
* @param {number} B Larger base
* @param {number} b Smaller base
* @param {number} height Trapezoid height
*
* @returns {number} ((B + b) * height) / 2
*/
trapezoid: (B, b, height) => {
return Math.floor((B + b) * height / 2);
},
/**
* @param {number} radius Circle radius
*
* @returns {number} π * (radius * radius)
*/
circle: (radius) => {
return Math.floor(PI * squared(radius));
}
};
// src/math/interpolation.ts
function getNewtonInterpolationB(input, end, start) {
if (end == start) {
return input.function(input.points[start]);
}
return (getNewtonInterpolationB(input, end, start + 1) - getNewtonInterpolationB(input, end - 1, start)) / (input.points[end] - input.points[start]);
}
function interpolation(input) {
if (input.points.length < 1) {
throw new Error("Points array must not be empty.");
}
let multiplier, output = input.function(input.points[0]);
for (let multiplierIndex, index = 1; index < input.points.length; index++) {
for (multiplier = 1, multiplierIndex = 0; multiplierIndex < index; multiplierIndex++) {
multiplier *= input.value - input.points[multiplierIndex];
}
output += multiplier * getNewtonInterpolationB(input, multiplierIndex, 0);
}
return output;
}
// src/math/regression.ts
function computePolynomialRegressionMatrix(matrix) {
let pivot, eliminationPivot;
for (let i = 0, j, k; i < 3; i++) {
const pivotStartIndex = i * 4;
pivot = matrix[pivotStartIndex + i];
for (j = 0; j < 4; j++) {
matrix[pivotStartIndex + j] /= pivot;
}
for (j = 0; j < 3; j++) {
if (j == i) {
continue;
}
const nonPivotStartIndex = j * 4;
eliminationPivot = matrix[nonPivotStartIndex + i];
for (k = i; k < 4; k++) {
matrix[nonPivotStartIndex + k] -= eliminationPivot * matrix[pivotStartIndex + k];
}
}
}
}
function regression(input) {
if (input.length === 0) {
throw new Error("Input array must not be empty.");
}
let x, y, mDivisor, sumX = 0, sumY = 0, sumXY = 0, sumXSquared = 0, sumXCubed = 0, sumXPower4 = 0, sumXSquaredY = 0;
for (let index = 0; index < input.length; index++) {
x = input[index][0];
y = input[index][1];
sumX += x;
sumY += y;
sumXY += x * y;
sumXSquared += x * x;
sumXCubed += x * x * x;
sumXPower4 += x * x * x * x;
sumXSquaredY += x * x * y;
}
const m = (mDivisor = input.length * sumXSquared - sumX * sumX) === 0 ? 0 : (input.length * sumXY - sumX * sumY) / mDivisor;
const b = (sumY - m * sumX) / input.length;
const polMatrix = [
input.length,
sumX,
sumXSquared,
sumY,
sumX,
sumXSquared,
sumXCubed,
sumXY,
sumXSquared,
sumXCubed,
sumXPower4,
sumXSquaredY
];
computePolynomialRegressionMatrix(polMatrix);
return {
linear: {
m,
b
},
polynomial: {
a: polMatrix[3],
b: polMatrix[7],
c: polMatrix[11]
}
};
}
// src/geography/population.ts
function populationDensity(population2, area2) {
return Math.max(population2 / area2);
}
function birthRate(birth, population2) {
return Math.max(birth * 1e3 / population2);
}
function deathRate(death, population2) {
return birthRate(death, population2);
}
var population = {
br: {
sp: 11452e3
// IBGE 2022
},
us: {
ny: 20201249
// USCB 2023
}
};
// src/geography/calculations.ts
var toRadians = (degrees) => degrees * (Math.PI / 180);
var toDegrees = (radians) => radians * (180 / Math.PI);
var haversineDistance = (lat1, lon1, lat2, lon2, radius = 6371) => {
const dLat = toRadians(lat2 - lat1);
const dLon = toRadians(lon2 - lon1);
const radLat1 = toRadians(lat1);
const radLat2 = toRadians(lat2);
const a = Math.sin(dLat / 2) ** 2 + Math.cos(radLat1) * Math.cos(radLat2) * Math.sin(dLon / 2) ** 2;
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return radius * c;
};
var calculateBearing = (lat1, lon1, lat2, lon2) => {
const radLat1 = toRadians(lat1);
const radLat2 = toRadians(lat2);
const dLon = toRadians(lon2 - lon1);
const y = Math.sin(dLon) * Math.cos(radLat2);
const x = Math.cos(radLat1) * Math.sin(radLat2) - Math.sin(radLat1) * Math.cos(radLat2) * Math.cos(dLon);
let bearing = toDegrees(Math.atan2(y, x));
bearing = (bearing + 360) % 360;
return bearing;
};
var midpoint = (lat1, lon1, lat2, lon2) => {
const radLat1 = toRadians(lat1);
const radLat2 = toRadians(lat2);
const dLon = toRadians(lon2 - lon1);
const Bx = Math.cos(radLat2) * Math.cos(dLon);
const By = Math.cos(radLat2) * Math.sin(dLon);
const midLat = Math.atan2(
Math.sin(radLat1) + Math.sin(radLat2),
Math.sqrt((Math.cos(radLat1) + Bx) ** 2 + By ** 2)
);
const midLon = toRadians(lon1) + Math.atan2(By, Math.cos(radLat1) + Bx);
return {
latitude: toDegrees(midLat),
longitude: toDegrees(midLon)
};
};
export {
PI,
area,
birthRate,
calculateBearing,
cathetus,
deathRate,
difference,
force,
haversineDistance,
hypotenuse,
interpolation,
isEven,
isOdd,
kineticEnergy,
midpoint,
momentum,
population,
populationDensity,
potentialEnergy,
regression,
squared,
toCelsius,
toFahrenheit,
toKelvin
};