> For the complete documentation index, see [llms.txt](https://apphp.gitbook.io/artificial-intelligence-with-php/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://apphp.gitbook.io/artificial-intelligence-with-php/machine-learning/mathematics-for-ml/linear-algebra/scalars/definition-and-operations.md).

# Definition and Operations

### Scalars: The Simplest Mathematical Objects

#### Definition

A scalar is a single numerical value. It can be a real number (like **3**, **-0.5**, or **π**) or a complex number (like **2 + 3i**).

In PHP it can be written like a constant or a variable:

```php
const MY_SCALAR = 1;
$x = 2;
$y = 3;
```

#### Properties

* Scalars have magnitude but no direction.
* They can be used to scale (multiply) other mathematical objects.

#### Examples in Real-World Applications

* Temperature readings
* Price of a stock
* Speed of a car

### Scalar Operations with PHP

In PHP it can be written as follows:

#### Basic Arithmetic Operations

```php
$a = 5;
$b = 2;

echo $a + $b;  // Addition: 7
echo $a - $b;  // Subtraction: 3
echo $a * $b;  // Multiplication: 10
echo $a / $b;  // Division: 2.5
echo $a % $b;  // Modulus: 1
echo $a ** $b; // Exponentiation: 25
```

#### Scalar-Vector Operations

```php
$scalar = 2;
$vector = [1, 2, 3];

// Scalar-vector multiplication
$result = array_map(function($x) use ($scalar) {
    return $x * $scalar;
}, $vector);
print_r($result); // Output: Array ( [0] => 2 [1] => 4 [2] => 6 )

// Scalar-vector addition
$result = array_map(function($x) use ($scalar) {
    return $x + $scalar;
}, $vector);
print_r($result); // Output: Array ( [0] => 3 [1] => 4 [2] => 5 )
```

#### Scalar Functions

```php
$x = -3.7;

echo abs($x);       // Absolute value: 3.7
echo ceil($x);      // Ceiling: -3
echo floor($x);     // Floor: -4
echo round($x);     // Round: -4
echo exp($x);       // Exponential: 0.024787521766663
echo log($x);       // Natural logarithm: NAN (Not a Number, as log of negative is undefined)
echo sqrt(abs($x)); // Square root of absolute value: 1.9235384061671
```

#### Trigonometric Operations

```php
$angle = M_PI / 4; // 45 degrees in radians

echo sin($angle); // Sine: 0.70710678118655
echo cos($angle); // Cosine: 0.70710678118655
echo tan($angle); // Tangent: 1
```

#### Random Number Generation

```php
echo rand(1, 10);  // Random integer between 1 and 10
echo mt_rand(1, 10);  // Faster random integer between 1 and 10
echo lcg_value();  // Random float between 0 and 1
```

#### Comparison Operations

```php
$a = 5;
$b = 3;

var_dump($a > $b);   // bool(true)
var_dump($a < $b);   // bool(false)
var_dump($a == $b);  // bool(false)
var_dump($a != $b);  // bool(true)
var_dump($a >= $b);  // bool(true)
var_dump($a <= $b);  // bool(false)
```

#### Bitwise Operations

```php
$a = 5;  // 101 in binary
$b = 3;  // 011 in binary

echo $a & $b;  // Bitwise AND: 1 (001 in binary)
echo $a | $b;  // Bitwise OR: 7 (111 in binary)
echo $a ^ $b;  // Bitwise XOR: 6 (110 in binary)
echo ~$a;      // Bitwise NOT: -6
echo $a << 1;  // Left shift: 10 (1010 in binary)
echo $a >> 1;  // Right shift: 2 (10 in binary)
```

These scalar operations form the foundation for more complex calculations in PHP, particularly in the context of mathematical and machine learning applications. They can be used to manipulate individual values, perform element-wise operations on arrays, and implement various algorithms.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://apphp.gitbook.io/artificial-intelligence-with-php/machine-learning/mathematics-for-ml/linear-algebra/scalars/definition-and-operations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
