Scalars
Scalar Operations with PHP
In PHP it can be written as a class Scalar with implementation of a set of scalar operations. This class is a PHP implementation of scalar operations commonly used in linear algebra and, by extension, in various AI and machine learning algorithms. It provides a robust set of methods for performing vectors calculations, making it a valuable tool for developers working on AI projects in PHP.
Example of use
<?php
use Apphp\MLKit\Math\Linear\Scalar;
// Example values
$a = 10;
$b = 3;
$x = -4.2;
$angle = M_PI / 4; // 45 degrees
$vector = [1, 2, 3];
// Arithmetic Operations
echo "Arithmetic Operations:\n---------\n";
echo "$a + $b = " . Scalar::add($a, $b) . "\n";
echo "$a - $b = " . Scalar::subtract($a, $b) . "\n";
echo "$a * $b = " . Scalar::multiply($a, $b) . "\n";
echo "$a / $b = " . Scalar::divide($a, $b) . "\n";
echo "$a % $b = " . Scalar::modulus($a, $b) . "\n";
echo "$a ^ $b = " . Scalar::power($a, $b) . "\n";
// Scalar-Vector Operations
echo "\nScalar-Vector Operations:\n---------\n";
echo '2 * [1, 2, 3] = ';
print_r(Scalar::multiplyVector(2, $vector));
echo '5 + [1, 2, 3] = ';
print_r(Scalar::addToVector(5, $vector));
// Rounding Operations
echo "\nRounding Operations:\n---------\n";
echo "ceil($x) = " . Scalar::ceiling($x) . "\n";
echo "floor($x) = " . Scalar::floor($x) . "\n";
echo "round($x) = " . Scalar::round($x) . "\n";
echo 'e^2 = ' . Scalar::exponential(2) . "\n";
echo 'ln(2.718) = ' . Scalar::logarithm(2.718) . "\n";
echo "√|$x| = ";
try {
echo Scalar::squareRoot($x) . "\n";
} catch (Exception $e) {
echo $e->getMessage() . "\n";
}
// Trigonometric Operations
echo "\nTrigonometric Operations:\n---------\n";
echo 'sin(π/4) = ' . Scalar::sine($angle) . "\n";
echo 'cos(π/4) = ' . Scalar::cosine($angle) . "\n";
echo 'tan(π/4) = ' . Scalar::tangent($angle) . "\n";
// Comparison Operations
echo "\nComparison Operations:\n---------\n";
echo "$a > $b = " . (Scalar::isGreaterThan($a, $b) ? 'true' : 'false') . "\n";
echo "$a < $b = " . (Scalar::isLessThan($a, $b) ? 'true' : 'false') . "\n";
echo "$a = $b = " . (Scalar::isEqual($a, $b) ? 'true' : 'false') . "\n";
// Bitwise Operations
echo "\nBitwise Operations:\n---------\n";
echo "$a << 2 = " . Scalar::leftShift($a, 2) . "\n";
echo "$a >> 1 = " . Scalar::rightShift($a, 1) . "\n";
// Random Number Generation
echo "\nRandom Number Generation:\n---------\n";
echo 'Random (0-100): ' . Scalar::randomInt(0, 100) . "\n";
echo 'MT Random (0-100): ' . Scalar::mtRandomInt(0, 100) . "\n";
echo 'LCG Value: ' . Scalar::lcgValue() . "\n";
// Examples with mixed types (integers and floats)
echo "\nMixed Type Examples:\n---------\n";
echo '5 + 2.5 = ' . Scalar::add(5, 2.5) . "\n";
echo '3 * 0.5 = ' . Scalar::multiply(3, 0.5) . "\n";
echo '2 ^ 1.5 = ' . Scalar::power(2, 1.5) . "\n";
echo '1.5 * [1, 2, 3] = ';
print_r(Scalar::multiplyVector(1.5, $vector));
// Precision Control Examples
echo "\nPrecision Control Examples:\n---------\n";
Scalar::setPrecision(3);
echo '1/3 (precision=3): ' . Scalar::divide(1, 3) . "\n";
Scalar::setOperationPrecision('basic_arithmetic', 4);
echo '1/3 (arithmetic precision=4): ' . Scalar::divide(1, 3) . "\n";
Scalar::resetPrecision();
Result:
Memory: 0.057 Mb
Time running: 0.002 sec.
Arithmetic Operations:
---------
10 + 3 = 13
10 - 3 = 7
10 * 3 = 30
10 / 3 = 3.33333
10 % 3 = 1
10 ^ 3 = 1000
Scalar-Vector Operations:
---------
2 * [1, 2, 3] = Array
(
[0] => 2
[1] => 4
[2] => 6
)
5 + [1, 2, 3] = Array
(
[0] => 6
[1] => 7
[2] => 8
)
Rounding Operations:
---------
ceil(-4.2) = -4
floor(-4.2) = -5
round(-4.2) = -4
e^2 = 7.3890561
ln(2.718) = 0.99989632
√|-4.2| = Square root argument must be non-negative. Got: -4.2
Trigonometric Operations:
---------
sin(π/4) = 0.70710678
cos(π/4) = 0.70710678
tan(π/4) = 1
Comparison Operations:
---------
10 > 3 = true
10 < 3 = false
10 = 3 = false
Bitwise Operations:
---------
10 << 2 = 40
10 >> 1 = 5
Random Number Generation:
---------
Random (0-100): 66
MT Random (0-100): 56
LCG Value: 0.79039053165526
Mixed Type Examples:
---------
5 + 2.5 = 7.5
3 * 0.5 = 1.5
2 ^ 1.5 = 2.82842712
1.5 * [1, 2, 3] = Array
(
[0] => 1.5
[1] => 3
[2] => 4.5
)
Precision Control Examples:
---------
1/3 (precision=3): 0.333
1/3 (arithmetic precision=4): 0.3333