NumPHP\Core\NumPHP::linspace PHP Метод

linspace() публичный статический Метод

Creates a vector (NumArray) from $low to $high with the size of $number
С версии: 1.0.0
public static linspace ( float $low, float $high, integer $number ) : NumArray
$low float beginning of the vector
$high float end of the vector
$number integer size of the vector
Результат NumArray
    public static function linspace($low, $high, $number)
    {
        if ($number < 0) {
            throw new InvalidArgumentException('Number has to be a positive value');
        }
        $data = [];
        switch ($number) {
            case 0:
                break;
            case 1:
                $data = [$low];
                break;
            default:
                $step = ($high - $low) / ($number - 1);
                $sum = $low;
                for ($i = 0; $i < $number; $i++) {
                    $data[] = $sum;
                    $sum += $step;
                }
                break;
        }
        return new NumArray($data);
    }

Usage Example

Пример #1
0
 /**
  * Test if InvalidArgumentException will be thrown, when using NumPHP::linspace
  * with negative `$number`
  *
  * @expectedException        \NumPHP\Core\Exception\InvalidArgumentException
  * @expectedExceptionMessage Number has to be a positive value
  */
 public function testLinspaceInvalidArgumentException()
 {
     NumPHP::linspace(1.5, 4.5, -1);
 }