skijs
v1.2.8
Published
SkiJS is an scientific computing language with several modules including advanced and basics operations.
Downloads
634
Maintainers
Readme
SkiJS
SkiJS is a JavaScript library that provides a collection of mathematical, physical, statistical, and scientific utilities. It includes modules for basic math, physics, linear algebra, optimization, calculus, unit conversions, probability, and more.
Installation
To install SkiJS using npm:
npm install skijs
Modules
math - Mathematical operations, such as addition, subtraction, multiplication, division and square root. geometry - Includes geometric calculations like the area of a circle and the volume of a sphere. physics - Performs basic physics calculations such as speed and acceleration. stats - Provides statistical operations like mean and median. linalg - Implements basic linear algebra operations such as matrix multiplication and determinant calculation. optimization - Provides optimization algorithms lime gradient descent for finding the minium of a function. probability - Provides functions for generating random numbers and distributions. thermodynamics - Implements thermodynamic calculations such as heat transfer and the ideal gas law. unitCorvension - Converts between different units of measurement. chemistry - Implements basic chemistry-related calculations such as molarity and gas pressure. astronomy - Provides astronomical calculations such as weight on different planets and escape velocity. calculus - Includes badic calculus functions like differentiation and integration. color - Handles color conversions between RGB and HEX formats. date - Provides functions for working with dates and times. geolocation - Provides geolocation calculations like the distance between two points on the Earth. dataProcessing - Provides utilities to manipulate, process and analyze datasets. Particularly arrays and objects. It includes functions for normalization, identifying outliers, grouping objects, sorting, calculating sums and ranges, and determining frequency distributions. It is designed to handle both numerical arrays and arrays of objects for broader applicability in data processing tasks. formatting - Provides utilities for formatting and transforming data into human-readable forms. It includes functions for adding commas to large numbers, converting bytes to human-readable sizes, formatting timestamps into human-readable durations, truncating long strings, formatting dates, and converting strings to title case. validation - The validation module provides utility functions to validate various types of data, ensuring they meet specific criteria such as being non-empty strings, valid email formats, numbers, or integers. It is useful for checking input validity and preventing errors in applications.
Usage
Below is an usage example.
const { math, physics, stats, geometry } = require('skijs');
console.log(math.addition(5, 3));
console.log(physics.speed(100, 20));
console.log(stats.mean([1, 2, 3, 4, 5]));
console.log(geometry.circleArea(5));
math example usage:
const { math } = require('skijs');
console.log("Math Operations");
console.log("Addition (10 + 7):", math.add(10, 7));
console.log("Subtraction (20 - 8):", math.subtract(20, 8));
console.log("Multiplication (4 * 6):", math.multiply(4, 6));
console.log("Division (36 / 9):", math.divide(36, 9));
console.log("Square Root of 25:", math.squareRoot(25));
physics example usage:
const { physics } = require('skijs');
console.log("\nPhysics Calculations");
console.log("Speed (distance=200, time=40):", physics.speed(200, 40));
console.log("Acceleration (finalVelocity=50, initialVelocity=10, time=5):", physics.acceleration(50, 10, 5));
stats example usage:
const { stats } = require('skijs');
console.log("\nStatistics Calculations");
console.log("Mean of [10, 20, 30, 40, 50]:", stats.mean([10, 20, 30, 40, 50]));
console.log("Median of [10, 20, 30, 40, 50]:", stats.median([10, 20, 30, 40, 50]));
lingalg example usage:
const { linalg } = require('skijs');
console.log("\nLinear Algebra Operations");
console.log("Matrix Multiplication:", linalg.matrixMultiply([[1, 3], [2, 4]], [[5, 7], [6, 8]]));
console.log("Determinant of [[2, 3], [4, 5]]:", linalg.determinant([[2, 3], [4, 5]]));
optimization example usage:
const { optimization } = require('skijs');
console.log("\nOptimization (Gradient Descent)");
console.log("Gradient Descent Result (Minimum):", optimization.gradientDescent(10, 0.05, 100, (x) => x * x - 3 * x));
calculus example usage:
const { calculus } = require('skijs');
console.log("\nCalculus Operations");
console.log("Derivative of f(x) = x^3 at x=2:", calculus.derivative(x => x ** 3, 2));
console.log("Integral of f(x) = 2x from 0 to 4:", calculus.integrate(x => 2 * x, 0, 4));
console.log("Trapezoidal Integration of f(x) = 2x from 0 to 4:", calculus.trapezoidalIntegration(x => 2 * x, 0, 4));
console.log("Simpson's Integration of f(x) = x^2 from 0 to 2:", calculus.simpsonsIntegration(x => x ** 2, 0, 2));
console.log("Mean Value Theorem for f(x) = x^2 from 0 to 2:", calculus.meanValueTheorem(x => x ** 2, 0, 2));
unitConversion example usage:
const { unitConversion } = require('skijs');
console.log("\nUnit Conversion");
console.log("5000 meters to kilometers:", unitConversion.metersToKilometers(5000));
console.log("5 kilograms to pounds:", unitConversion.kilogramsToPounds(5));
probability example usage:
const { probability } = require('skijs');
console.log("\nProbability Calculations");
console.log("Uniform Random (0 to 50):", probability.uniformRandom(0, 50));
console.log("Normal Random (mean=5, stdDev=2):", probability.normalRandom(5, 2));
geometry example usage:
const { geometry } = require('skijs');
console.log("\nGeometry Calculations");
console.log("Area of Circle (radius=7):", geometry.areaCircle(7));
console.log("Volume of Sphere (radius=7):", geometry.volumeSphere(7));
astronomy example usage:
const { astronomy } = require('skijs');
console.log("\nAstronomy Calculations");
console.log("Weight on Jupiter (mass=60kg, gravity=24.79):", astronomy.weightOnPlanet(60, 24.79));
console.log("Escape Velocity of Mars (mass=0.64171e24, radius=3.3962e6):", astronomy.escapeVelocity(0.64171e24, 3.3962e6));
chemistry example usage:
const { chemistry } = require('skijs');
console.log("\nChemistry Calculations");
console.log("Molarity (moles=3, volume=2L):", chemistry.molarity(3, 2));
console.log("Gas Pressure (V=2L, n=2mol, T=298K):", chemistry.gasPressure(2, 2, 298));
date example usage:
const { date } = require('skijs');
console.log("\nDate and Time Operations");
console.log("Current Date and Time:", date.currentDateTime());
console.log("Formatted Date:", date.formatDate(new Date()));
color example usage:
const { color } = require('skijs');
console.log("\nColor operations");
console.log("RGB to HEX (255, 0, 0):", color.rgbToHex(255, 0, 0));
console.log("RGB to HEX (0, 255, 0):", color.rgbToHex(0, 255, 0));
console.log("RGB to HEX (0, 0, 255):", color.rgbToHex(0, 0, 255));
console.log("HEX to RGB (#ff0000):", color.hexToRgb("#ff0000"));
console.log("HEX to RGB (#00ff00):", color.hexToRgb("#00ff00"));
console.log("HEX to RGB (#0000ff):", color.hexToRgb("#0000ff"));
console.log("Random color:", color.randomColor());
console.log("Blend red (#ff0000) and blue (#0000ff) with ratio 0.5:", color.blendColors("#ff0000", "#0000ff"));
console.log("Blend red (#ff0000) and green (#00ff00) with ratio 0.3:", color.blendColors("#ff0000", "#00ff00", 0.3));
console.log("Lighten red (#ff0000) by 20%:", color.lightenColor("#ff0000", 0.2));
console.log("Darken green (#00ff00) by 20%:", color.darkenColor("#00ff00", 0.2));
console.log("RGB to HSL (255, 0, 0):", color.rgbToHsl(255, 0, 0));
console.log("RGB to HSL (0, 255, 0):", color.rgbToHsl(0, 255, 0));
console.log("RGB to HSL (0, 0, 255):", color.rgbToHsl(0, 0, 255));
console.log("HSL to RGB (0, 100, 50):", color.hslToRgb(0, 100, 50));
console.log("HSL to RGB (120, 100, 50):", color.hslToRgb(120, 100, 50));
console.log("HSL to RGB (240, 100, 50):", color.hslToRgb(240, 100, 50));
geolocation example usage:
const { geolocation } = require('skijs');
console.log("\nGeolocation Calculations");
console.log("Distance between New York and Los Angeles:", geolocation.haversine(40.7128, -74.0060, 34.0522, -118.2437));
console.log("Midpoint between New York and Los Angeles:", geolocation.midpoint(40.7128, -74.0060, 34.0522, -118.2437));
thermodynamics example usage:
const { thermodynamics } = require('skijs');
console.log("\nThermodynamics Calculations");
console.log("Heat Transfer (mass=5kg, specificHeat=4186, deltaT=20):", thermodynamics.heatTransfer(5, 4186, 20));
console.log("Ideal Gas Law (P=?, V=2m³, n=2mol, T=350K):", thermodynamics.idealGasLaw(null, 2, 350));
dataProcessing example usage:
const { dataProcessing } = require('skijs');
console.log("\nData Processing"); const numbers = [1, 2, 3, 4, 100, 5, 6];
const objects = [ { name: "Alice", age: 30 },
{ name: "Bob", age: 25 }, { name: "Charlie", age: 30 }
];
console.log("Normalized Data:", dataProcessing.normalize(numbers));
console.log("Outliers:", dataProcessing.findOutliers(numbers));
console.log("Grouped by Age:", dataProcessing.groupBy(objects, 'age'));
console.log("Sorted by Name:", dataProcessing.sortBy(objects, 'name'));
console.log("Sum:", dataProcessing.sum(numbers));
console.log("Range:", dataProcessing.range(numbers)); console.log("Frequency:", dataProcessing.frequency(numbers));
formatting example usage:
const { formatting } = require('skijs');
console.log("Number with commas:", formatting.numberWithCommas(1234567));
console.log("Bytes to Size:", formatting.bytesToSize(1048576));
console.log("Human readable time:", formatting.humanReadableTime(3661000));
console.log("Truncate string:", formatting.truncateString("This is a long string", 20));
console.log("Formatted date:", formatting.formatDate(new Date()));
console.log("Title case:", formatting.toTitleCase("this is a test string"));
validation example usage:
const { validation } = require('skijs');
console.log("Is Non-Empty String ('Hello'):", validation.isNonEmptyString('Hello'));
console.log("Is Non-Empty String (''): ", validation.isNonEmptyString(''));
console.log("Is Valid Email ('[email protected]'):", validation.isValidEmail('[email protected]'));
console.log("Is Valid Email ('invalid-email'):", validation.isValidEmail('invalid-email'));
console.log("Is Number (123):", validation.isNumber(123));
console.log("Is Number ('abc'):", validation.isNumber('abc'));
console.log("Is Integer (42):", validation.isInteger(42));
console.log("Is Integer (3.14):", validation.isInteger(3.14));
console.log("Is Integer ('42'):", validation.isInteger('42'));
console.log("Is Integer (null):", validation.isInteger(null));
console.log("Is Boolean (true):", validation.isBoolean(true));
console.log("Is Boolean (false):", validation.isBoolean(false));
console.log("Is Boolean (null):", validation.isBoolean(null));
console.log("Is Array ([1, 2, 3]):", validation.isArray([1, 2, 3]));
console.log("Is Array({ key: 'value' }):", validation.isArray({ key: 'value' }));
console.log("Is Date (new Date()):", validation.isDate(new Date()));
console.log("Is Date ('2024-12-30'):", validation.isDate('2024-12-30'));
console.log("Is URL ('https://example.com'):", validation.isUrl('https://example.com'));
console.log("Is URL ('not-a-url'):", validation.isUrl('not-a-url'));
console.log("Is Object ({ key: 'value' }):", validation.isObject({ key: 'value' }));
console.log("Is Object ([1, 2, 3]):", validation.isObject([1, 2, 3]));
Licensing
This project is licensed under the MIT License. See the LICENSE file for more details.