danyseif/calcify

v1.0.0 2024-09-02 10:15 UTC

This package is auto-updated.

Last update: 2024-10-02 10:38:29 UTC


README

Calcify 是一个提供各种数学和统计函数的 PHP 软件包。它包括执行多项式、矩阵和算术运算的类。还包括用于统计计算(如均值、中位数、众数、方差和标准差)的接口。该软件包旨在用于 Laravel 应用程序,并且可以通过 Composer 安装。

功能

  • 多项式运算:加法、减法、乘法、除法、评估和求根。
  • 矩阵运算:加法、减法、乘法、转置、求逆、求行列式、求秩、LU 分解、Cholesky 分解、Hadamard 积、Kronecker 积、求迹和矩阵格式化。
  • 算术运算:加法、减法、乘法、除法、求模、乘方、求根、对数、三角函数、双曲函数、阶乘、绝对值和百分比计算。
  • 统计计算:均值、中位数、众数、范围、方差、标准差、样本方差、样本标准差、变异系数和加权均值。

安装

您可以通过 Composer 安装此软件包

composer require danyseif/calcify

用法

多项式运算

use Danyseif\Calcify\Base\Polynomial;

$polynomial1 = new Polynomial([1, 2, 3]); // 1x^2 + 2x + 3
$polynomial2 = new Polynomial([4, 5, 6]); // 4x^2 + 5x + 6

$sum = $polynomial1->add($polynomial2);
echo $sum; // 5x^2 + 7x + 9

//  Available methods:
// * - `add(Polynomial $other): Polynomial` - Add two polynomials.
// * - `subtract(Polynomial $other): Polynomial` - Subtract one polynomial from another.
// * - `multiply(Polynomial $other): Polynomial` - Multiply two polynomials.
// * - `divide(Polynomial $other): array` - Divide the polynomial by another polynomial.
// * - `evaluate(float $x): float` - Evaluate the polynomial at a given value.
// * - `degree(): int` - Get the degree of the polynomial.
// * - `derivative(): Polynomial` - Get the derivative of the polynomial.
// * - `findRoots(): array` - Find the roots of the polynomial (only for quadratic polynomials).
// * - `isZero(): bool` - Check if the polynomial is zero.
// * - `isConstant(): bool` - Check if the polynomial is a constant.
// * - `isLinear(): bool` - Check if the polynomial is linear.
// * - `isQuadratic(): bool` - Check if the polynomial is quadratic.
// * - `leadingCoefficient(): float` - Get the leading coefficient of the polynomial.
// * - `constantTerm(): float` - Get the constant term of the polynomial.

矩阵运算

use Danyseif\Calcify\Base\Matrix;

$matrixOps = new MatrixOperations();

$matrixA = [
[1, 2],
[3, 4]
];
$matrixB = [
[5, 6],
[7, 8]
];


$sum = $matrixOps->add($matrixA, $matrixB);
$diff = $matrixOps->subtract($matrixA, $matrixB);
$product = $matrixOps->multiply($matrixA, $matrixB);
$transpose = $matrixOps->transpose($matrixA);
$inverse = $matrixOps->inverse($matrixA);
$determinant = $matrixOps->determinant($matrixA);
$rank = $matrixOps->rank($matrixA);
$luDecomposition = $matrixOps->luDecomposition($matrixA);
$choleskyDecomposition = $matrixOps->choleskyDecomposition($matrixA);
$hadamardProduct = $matrixOps->hadamardProduct($matrixA, $matrixB);
$kroneckerProduct = $matrixOps->kroneckerProduct($matrixA, $matrixB);
$trace = $matrixOps->trace($matrixA);
$formattedMatrix = $matrixOps->formatMatrix($matrixA);

// * Available methods:
// * - `add(array $matrixA, array $matrixB): array`
// * - `subtract(array $matrixA, array $matrixB): array`
// * - `multiply(array $matrixA, array $matrixB): array`
// * - `transpose(array $matrix): array`
// * - `inverse(array $matrix): array`
// * - `determinant(array $matrix): float`
// * - `rank(array $matrix): int`
// * - `luDecomposition(array $matrix): array`
// * - `choleskyDecomposition(array $matrix): array`
// * - `hadamardProduct(array $matrixA, array $matrixB): array`
// * - `kroneckerProduct(array $matrixA, array $matrixB): array`
// * - `trace(array $matrix): float|int`
// * - `formatMatrix(array $matrix): string`

算术运算

use Danyseif\Calcify\Base\Arithmetic;

$arithmetic = new Arithmetic(10);
$result = $arithmetic->add(5)
->subtract(3)
->multiply(2)
->divide(4)
->getResult();

//  * Available methods:

//  * - `create(float $initialValue, bool $isRounded, string $roundingMode, int $decimalPlaces): self`
//  * - `enableRounding(string $roundingMode): self`
//  * - `disableRounding(int $decimalPlaces): self`
//  * - `add($value): self`
//  * - `subtract($value): self`
//  * - `multiply($value): self`
//  * - `divide($value): self`
//  * - `modulo($value): self`
//  * - `power($value): self`
//  * - `root($value): self`
//  * - `logarithm($base): self`
//  * - `naturalLogarithm(): self`
//  * - `commonLogarithm(): self`
//  * - `exponential(): self`
//  * - `sine(): self`
//  * - `cosine(): self`
//  * - `tangent(): self`
//  * - `cotangent(): self`
//  * - `secant(): self`
//  * - `cosecant(): self`
//  * - `arcsine(): self`
//  * - `arccosine(): self`
//  * - `arctangent(): self`
//  * - `arccotangent(): self`
//  * - `arcsecant(): self`
//  * - `arccosecant(): self`
//  * - `hyperbolicSine(): self`
//  * - `hyperbolicCosine(): self`
//  * - `hyperbolicTangent(): self`
//  * - `hyperbolicCotangent(): self`
//  * - `hyperbolicSecant(): self`
//  * - `hyperbolicCosecant(): self`
//  * - `inverseHyperbolicSine(): self`
//  * - `inverseHyperbolicCosine(): self`
//  * - `inverseHyperbolicTangent(): self`
//  * - `inverseHyperbolicCotangent(): self`
//  * - `inverseHyperbolicSecant(): self`
//  * - `inverseHyperbolicCosecant(): self`
//  * - `factorial(): self`
//  * - `absolute(): self`
//  * - `maximum($value): self`
//  * - `minimum($value): self`
//  * - `percentage($percent): self`
//  * - `addPercentage($percent): self`
//  * - `subtractPercentage($percent): self`
//  * - `discount($percent): self`
//  * - `increaseByPercentage($percent): self`
//  * - `decreaseByPercentage($percent): self`
//  * - `getResult(): float`
//  * - `reset($value): self`
//  * - `__toString(): string`
//  * - `getFormattedResult(string $format): string|int`
//  * - `__invoke(): float`

统计计算

use Danyseif\Calcify\Base\Statistics;
$stats = new Statistics();
$numbers = [1, 2, 3, 4, 5];
$mean = $stats->mean($numbers);
$median = $stats->median($numbers);
$mode = $stats->mode($numbers);
$range = $stats->range($numbers);
$variance = $stats->variance($numbers);
$stdDev = $stats->standardDeviation($numbers);
$sampleVariance = $stats->sampleVariance($numbers);
$sampleStdDev = $stats->sampleStandardDeviation($numbers);
$coefficientOfVariation = $stats->coefficientOfVariation($numbers);
$weightedMean = $stats->weightedMean($numbers, [1, 1, 1, 1, 1]);

//  * Available methods:
//  * - `mean(array $numbers): float` - Calculate the mean of an array of numbers.
//  * - `median(array $numbers): float` - Calculate the median of an array of numbers.
//  * - `mode(array $numbers): array` - Calculate the mode of an array of numbers.
//  * - `range(array $numbers): float` - Calculate the range of an array of numbers.
//  * - `variance(array $numbers): float` - Calculate the variance of an array of numbers.
//  * - `standardDeviation(array $numbers): float` - Calculate the standard deviation of an array of numbers.
//  * - `sampleVariance(array $numbers): float` - Calculate the sample variance of an array of numbers.
//  * - `sampleStandardDeviation(array $numbers): float` - Calculate the sample standard deviation of an array of numbers.
//  * - `coefficientOfVariation(array $numbers): float` - Calculate the coefficient of variation of an array of numbers.
//  * - `weightedMean(array $numbers, array $weights): float` - Calculate the weighted mean of an array of numbers with corresponding weights.

Laravel 集成

Calcify 可以轻松集成到 Laravel 应用程序中。该软件包包括一个服务提供者,可以在您的 config/app.php 文件中注册

use Danyseif\Calcify\Providers\CalcifyServiceProvider;

return [
    // Other service providers...
    CalcifyServiceProvider::class,
]

贡献

欢迎贡献!请随时提交 Pull Request。

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件

作者

Dany Seifeddine