MATHPHP-强大的现代数学库用于PHP
您需要将数学功能集成到应用程序中的唯一库。它是一个纯PHP中的独立库,没有外部依赖性。
特征
- 代数
- 算术
- 表达
- 多项式
- 金融
- 功能
- 地图
- 特殊功能
- 信息理论
- 熵
- 线性代数
- 矩阵
- 向量
- 数字
- 任意整数
- 复杂的
- 季节
- 合理的
- 数字理论
- 整数
- 数值分析
- 插值
- 数值差异化
- 数值集成
- 根发现
- 可能性
- 组合学
- 分布
- 连续的
- 离散的
- 多变量
- 表
- 示例数据
- 搜索
- 序列
- 基本的
- 先进的
- 非企业
- 集理论
- 统计数据
- 方差分析
- 平均值
- 圆
- 相关性
- 描述性
- 距离
- 分布
- 分歧
- 效果大小
- 实验
- 内核密度估计
- 多变量
- PCA(主要组件分析)
- PLS(部分最小二乘回归)
- 离群值
- 随机变量
- 回归
- 显着性测试
- 三角学
设置
将库添加到您的composer.json文件中:
{ \"require\" : { \"markrogoyski/math-php\" : \"2.*\" } }
使用作曲家安装库:
$ php composer.phar install
作曲家将在供应商文件夹中安装MathPHP。然后,您可以将以下内容添加到.php文件中以使用自动加载的库。
require_once __DIR__ . \' /vendor/autoload.php \' ;
另外,请在命令行上使用Composer来要求并安装MathPHP:
$ php composer.phar require markrogoyski/math-php:2.*
最低要求
- PHP 7.2
注意:对于PHP 7.0和7.1,请使用V1.0( markrogoyski/math-php:1.* )
用法
代数
use MathPHP \\ Algebra ; // Greatest common divisor (GCD) $ gcd = Algebra:: gcd ( 8 , 12 ); // Extended greatest common divisor - gcd(a, b) = a*a\' + b*b\' $ gcd = Algebra:: extendedGcd ( 12 , 8 ); // returns array [gcd, a\', b\'] // Least common multiple (LCM) $ lcm = Algebra:: lcm ( 5 , 2 ); // Factors of an integer $ factors = Algebra:: factors ( 12 ); // returns [1, 2, 3, 4, 6, 12] // Linear equation of one variable: ax + b = 0 [ $ a , $ b ] = [ 2 , 4 ]; // 2x + 4 = 0 $ x = Algebra:: linear ( $ a , $ b ); // Quadratic equation: ax² + bx + c = 0 [ $ a , $ b , $ c ] = [ 1 , 2 , - 8 ]; // x² + 2x - 8 [ $ x₁ , $ x₂ ] = Algebra:: quadratic ( $ a , $ b , $ c ); // Discriminant: Δ = b² - 4ac [ $ a , $ b , $ c ] = [ 2 , 3 , 4 ]; // 3² - 4(2)(4) $ Δ = Algebra:: discriminant ( $ a , $ b , $ c ); // Cubic equation: z³ + a₂z² + a₁z + a₀ = 0 [ $ a₃ , $ a₂ , $ a₁ , $ a₀ ] = [ 2 , 9 , 3 , - 4 ]; // 2x³ + 9x² + 3x -4 [ $ x₁ , $ x₂ , $ x₃ ] = Algebra:: cubic ( $ a₃ , $ a₂ , $ a₁ , $ a₀ ); // Quartic equation: a₄z⁴ + a₃z³ + a₂z² + a₁z + a₀ = 0 [ $ a₄ , $ a₃ , $ a₂ , $ a₁ , $ a₀ ] = [ 1 , - 10 , 35 , - 50 , 24 ]; // z⁴ - 10z³ + 35z² - 50z + 24 = 0 [ $ z₁ , $ z₂ , $ z₃ , $ z₄ ] = Algebra:: quartic ( $ a₄ , $ a₃ , $ a₂ , $ a₁ , $ a₀ );
算术
use MathPHP \\ Arithmetic ; $ √x = Arithmetic:: isqrt ( 8 ); // 2 Integer square root $ ³√x = Arithmetic:: cubeRoot (- 8 ); // -2 $ ⁿ√x = Arithmetic:: root ( 81 , 4 ); // nᵗʰ root (4ᵗʰ): 3 // Sum of digits $ digit_sum = Arithmetic:: digitSum ( 99 ); // 18 $ digital_root = Arithmetic:: digitalRoot ( 99 ); // 9 // Equality of numbers within a tolerance $ x = 0.00000003458 ; $ y = 0.00000003455 ; $ ε = 0.0000000001 ; $ almostEqual = Arithmetic:: almostEqual ( $ x , $ y , $ ε ); // true // Copy sign $ magnitude = 5 ; $ sign = - 3 ; $ signed_magnitude = Arithmetic:: copySign ( $ magnitude , $ sign ); // -5 // Modulo (Differs from PHP remainder (%) operator for negative numbers) $ dividend = 12 ; $ divisor = 5 ; $ modulo = Arithmetic:: modulo ( $ dividend , $ divisor ); // 2 $ modulo = Arithmetic:: modulo (- $ dividend , $ divisor ); // 3
表达 – 多项式
use MathPHP \\ Expression \\ Polynomial ; // Polynomial x² + 2x + 3 $ coefficients = [ 1 , 2 , 3 ] $ polynomial = new Polynomial ( $ coefficients ); // Evaluate for x = 3 $ x = 3 ; $ y = $ polynomial ( $ x ); // 18: 3² + 2*3 + 3 // Calculus $ derivative = $ polynomial -> differentiate (); // Polynomial 2x + 2 $ integral = $ polynomial -> integrate (); // Polynomial ⅓x³ + x² + 3x // Arithmetic $ sum = $ polynomial -> add ( $ polynomial ); // Polynomial 2x² + 4x + 6 $ sum = $ polynomial -> add ( 2 ); // Polynomial x² + 2x + 5 $ difference = $ polynomial -> subtract ( $ polynomial ); // Polynomial 0 $ difference = $ polynomial -> subtract ( 2 ); // Polynomial x² + 2x + 1 $ product = $ polynomial -> multiply ( $ polynomial ); // Polynomial x⁴ + 4x³ + 10x² + 12x + 9 $ product = $ polynomial -> multiply ( 2 ); // Polynomial 2x² + 4x + 6 $ negated = $ polynomial -> negate (); // Polynomial -x² - 2x - 3 // Data $ degree = $ polynomial -> getDegree (); // 2 $ coefficients = $ polynomial -> getCoefficients (); // [1, 2, 3] // String representation print ( $ polynomial ); // x² + 2x + 3 // Roots $ polynomial = new Polynomial ([ 1 , - 3 , - 4 ]); $ roots = $ polynomial -> roots (); // [-1, 4] // Companion matrix $ companion = $ polynomial -> companionMatrix ();
金融
use MathPHP \\ Finance ; // Financial payment for a loan or annuity with compound interest $ rate = 0.035 / 12 ; // 3.5% interest paid at the end of every month $ periods = 30 * 12 ; // 30-year mortgage $ present_value = 265000 ; // Mortgage note of $265,000.00 $ future_value = 0 ; $ beginning = false ; // Adjust the payment to the beginning or end of the period $ pmt = Finance:: pmt ( $ rate , $ periods , $ present_value , $ future_value , $ beginning ); // Interest on a financial payment for a loan or annuity with compound interest. $ period = 1 ; // First payment period $ ipmt = Finance:: ipmt ( $ rate , $ period , $ periods , $ present_value , $ future_value , $ beginning ); // Principle on a financial payment for a loan or annuity with compound interest $ ppmt = Finance:: ppmt ( $ rate , $ period , $ periods , $ present_value , $ future_value = 0 , $ beginning ); // Number of payment periods of an annuity. $ periods = Finance:: periods ( $ rate , $ payment , $ present_value , $ future_value , $ beginning ); // Annual Equivalent Rate (AER) of an annual percentage rate (APR) $ nominal = 0.035 ; // APR 3.5% interest $ periods = 12 ; // Compounded monthly $ aer = Finance:: aer ( $ nominal , $ periods ); // Annual nominal rate of an annual effective rate (AER) $ nomial = Finance:: nominal ( $ aer , $ periods ); // Future value for a loan or annuity with compound interest $ payment = 1189.97 ; $ fv = Finance:: fv ( $ rate , $ periods , $ payment , $ present_value , $ beginning ) // Present value for a loan or annuity with compound interest $ pv = Finance:: pv ( $ rate , $ periods , $ payment , $ future_value , $ beginning ) // Net present value of cash flows $ values = [- 1000 , 100 , 200 , 300 , 400 ]; $ npv = Finance:: npv ( $ rate , $ values ); // Interest rate per period of an annuity $ beginning = false ; // Adjust the payment to the beginning or end of the period $ rate = Finance:: rate ( $ periods , $ payment , $ present_value , $ future_value , $ beginning ); // Internal rate of return $ values = [- 100 , 50 , 40 , 30 ]; $ irr = Finance:: irr ( $ values ); // Rate of return of an initial investment of $100 with returns of $50, $40, and $30 // Modified internal rate of return $ finance_rate = 0.05 ; // 5% financing $ reinvestment_rate = 0.10 ; // reinvested at 10% $ mirr = Finance:: mirr ( $ values , $ finance_rate ); // rate of return of an initial investment of $100 at 5% financing with returns of $50, $40, and $30 reinvested at 10% // Discounted payback of an investment $ values = [- 1000 , 100 , 200 , 300 , 400 , 500 ]; $ rate = 0.1 ; $ payback = Finance:: payback ( $ values , $ rate ); // The payback period of an investment with a $1,000 investment and future returns of $100, $200, $300, $400, $500 and a discount rate of 0.10 // Profitability index $ values = [- 100 , 50 , 50 , 50 ]; $ profitability_index = Finance:: profitabilityIndex ( $ values , $ rate ); // The profitability index of an initial $100 investment with future returns of $50, $50, $50 with a 10% discount rate
功能 – 地图 – 单个数组
use MathPHP \\ Functions \\ Map ; $ x = [ 1 , 2 , 3 , 4 ]; $ sums = Map \\Single:: add ( $ x , 2 ); // [3, 4, 5, 6] $ differences = Map \\Single:: subtract ( $ x , 1 ); // [0, 1, 2, 3] $ products = Map \\Single:: multiply ( $ x , 5 ); // [5, 10, 15, 20] $ quotients = Map \\Single:: divide ( $ x , 2 ); // [0.5, 1, 1.5, 2] $ x² = Map \\Single:: square ( $ x ); // [1, 4, 9, 16] $ x³ = Map \\Single:: cube ( $ x ); // [1, 8, 27, 64] $ x⁴ = Map \\Single:: pow ( $ x , 4 ); // [1, 16, 81, 256] $ √x = Map \\Single:: sqrt ( $ x ); // [1, 1.414, 1.732, 2] $ ∣x∣ = Map \\Single:: abs ( $ x ); // [1, 2, 3, 4] $ maxes = Map \\Single:: max ( $ x , 3 ); // [3, 3, 3, 4] $ mins = Map \\Single:: min ( $ x , 3 ); // [1, 2, 3, 3] $ reciprocals = Map \\Single:: reciprocal ( $ x ); // [1, 1/2, 1/3, 1/4]
功能 – 地图 – 多个阵列
use MathPHP \\ Functions \\ Map ; $ x = [ 10 , 10 , 10 , 10 ]; $ y = [ 1 , 2 , 5 , 10 ]; // Map function against elements of two or more arrays, item by item (by item ...) $ sums = Map \\Multi:: add ( $ x , $ y ); // [11, 12, 15, 20] $ differences = Map \\Multi:: subtract ( $ x , $ y ); // [9, 8, 5, 0] $ products = Map \\Multi:: multiply ( $ x , $ y ); // [10, 20, 50, 100] $ quotients = Map \\Multi:: divide ( $ x , $ y ); // [10, 5, 2, 1] $ maxes = Map \\Multi:: max ( $ x , $ y ); // [10, 10, 10, 10] $ mins = Map \\Multi:: mins ( $ x , $ y ); // [1, 2, 5, 10] // All functions work on multiple arrays; not limited to just two $ x = [ 10 , 10 , 10 , 10 ]; $ y = [ 1 , 2 , 5 , 10 ]; $ z = [ 4 , 5 , 6 , 7 ]; $ sums = Map \\Multi:: add ( $ x , $ y , $ z ); // [15, 17, 21, 27]
功能 – 特殊功能
use MathPHP \\ Functions \\ Special ; // Gamma function Γ(z) $ z = 4 ; $ Γ = Special:: gamma ( $ z ); $ Γ = Special:: gammaLanczos ( $ z ); // Lanczos approximation $ Γ = Special:: gammaStirling ( $ z ); // Stirling approximation $ l = Special:: logGamma ( $ z ); $ c = Special:: logGammaCorr ( $ z ); // Log gamma correction // Incomplete gamma functions - γ(s,t), Γ(s,x), P(s,x) [ $ x , $ s ] = [ 1 , 2 ]; $ γ = Special:: lowerIncompleteGamma ( $ x , $ s ); $ Γ = Special:: upperIncompleteGamma ( $ x , $ s ); $ P = Special:: regularizedLowerIncompleteGamma ( $ x , $ s ); // Beta function [ $ x , $ y ] = [ 1 , 2 ]; $ β = Special:: beta ( $ x , $ y ); $ lβ = Special:: logBeta ( $ x , $ y ); // Incomplete beta functions [ $ x , $ a , $ b ] = [ 0.4 , 2 , 3 ]; $ B = Special:: incompleteBeta ( $ x , $ a , $ b ); $ Iₓ = Special:: regularizedIncompleteBeta ( $ x , $ a , $ b ); // Multivariate beta function $ αs = [ 1 , 2 , 3 ]; $ β = Special:: multivariateBeta ( $ αs ); // Error function (Gauss error function) $ error = Special:: errorFunction ( 2 ); // same as erf $ error = Special:: erf ( 2 ); // same as errorFunction $ error = Special:: complementaryErrorFunction ( 2 ); // same as erfc $ error = Special:: erfc ( 2 ); // same as complementaryErrorFunction // Hypergeometric functions $ pFq = Special:: generalizedHypergeometric ( $ p , $ q , $ a , $ b , $ c , $ z ); $ ₁F₁ = Special:: confluentHypergeometric ( $ a , $ b , $ z ); $ ₂F₁ = Special:: hypergeometric ( $ a , $ b , $ c , $ z ); // Sign function (also known as signum or sgn) $ x = 4 ; $ sign = Special:: signum ( $ x ); // same as sgn $ sign = Special:: sgn ( $ x ); // same as signum // Logistic function (logistic sigmoid function) $ x₀ = 2 ; // x-value of the sigmoid\'s midpoint $ L = 3 ; // the curve\'s maximum value $ k = 4 ; // the steepness of the curve $ x = 5 ; $ logistic = Special:: logistic ( $ x₀ , $ L , $ k , $ x ); // Sigmoid function $ t = 2 ; $ sigmoid = Special:: sigmoid ( $ t ); // Softmax function $ ? = [ 1 , 2 , 3 , 4 , 1 , 2 , 3 ]; $ σ⟮?⟯ⱼ = Special::softmax( $ ?); // Log of the error term in the Stirling-De Moivre factorial series $ err = Special:: stirlingError ( $ n );
信息理论 – 熵
use MathPHP \\ InformationTheory \\ Entropy ; // Probability distributions $ p = [ 0.2 , 0.5 , 0.3 ]; $ q = [ 0.1 , 0.4 , 0.5 ]; // Shannon entropy $ bits = Entropy:: shannonEntropy ( $ p ); // log₂ $ nats = Entropy:: shannonNatEntropy ( $ p ); // ln $ harts = Entropy:: shannonHartleyEntropy ( $ p ); // log₁₀ // Cross entropy $ H⟮p、q⟯ = Entropy:: crossEntropy ( $ p , $ q ); // log₂ // Joint entropy $ P⟮x、y⟯ = [ 1 / 2 , 1 / 4 , 1 / 4 , 0 ]; H⟮x、y⟯ = Entropy:: jointEntropy ( $ P⟮x、y⟯ ); // log₂ // Rényi entropy $ α = 0.5 ; $ Hₐ⟮X⟯ = Entropy:: renyiEntropy ( $ p , $ α ); // log₂ // Perplexity $ perplexity = Entropy:: perplexity ( $ p ); // log₂
线性代数 – 矩阵
下载源码
通过命令行克隆项目:
git clone https://github.com/markrogoyski/math-php.git
