NumPhp::tri

Construct an array with ones at and below the given diagonal and zeros elsewhere.

Description

SciPhp\NdArray NumPhp::tri( int $rows [, int $cols = null ] [, int $k = 0 ])

Parameters

$rows
A positive integer. Represents number of rows.
$cols
A positive integer. Represents number of columns.
$k
Default value is 0. It's an offset.

Return Values

A SciPhp\NdArray

Examples

Example #1: Construct a 4*4 array, offset=0

use SciPhp\NumPhp as np;

$m np::tri(4);

print 
$m;

The above example will output:

[[1  0  0  0]
 [1  1  0  0]
 [1  1  1  0]
 [1  1  1  1]]

Example #2: Construct a 3*5 array, offset=0

use SciPhp\NumPhp as np;

$m np::tri(35);

print 
$m;

The above example will output:

[[1  0  0  0  0]
 [1  1  0  0  0]
 [1  1  1  0  0]]

Example #3: Construct a 3*5 array, offset=1

use SciPhp\NumPhp as np;

$m np::tri(351);

print 
$m;

The above example will output:

[[1  1  0  0  0]
 [1  1  1  0  0]
 [1  1  1  1  0]]

Example #4: Construct a 3*5 array, offset=-1

use SciPhp\NumPhp as np;

$m np::tri(35, -1);

print 
$m;

The above example will output:

[[0  0  0  0  0]
 [1  0  0  0  0]
 [1  1  0  0  0]]

See Also

  • NumPhp::diag() - Extract a diagonal or construct a diagonal array.
  • NumPhp::eye() - Construct a 2-D array with ones on the diagonal and zeros elsewhere.
  • NumPhp::identity() - Construct a square 2-D array with ones on the diagonal and zeros elsewhere.
  • NumPhp::tril() - Construct a triangle matrix based on the lower triangle of another one.
  • NumPhp::triu() - Construct a triangle matrix based on the upper triangle of another one.
  • NumPhp::vander() - Generate a Vandermonde matrix.