github.com/djordje200179/extendedlibrary/datastructures@v1.7.1-0.20240227175559-d09520a92dd4/other/matrix/math_constructors.go (about)

     1  package matrix
     2  
     3  import "github.com/djordje200179/extendedlibrary/misc/math"
     4  
     5  // Zeros returns a matrix of the given size filled with zeros.
     6  func Zeros[T math.Number](size Size) *Matrix[T] {
     7  	return New[T](size)
     8  }
     9  
    10  // Ones returns a matrix of the given size filled with ones.
    11  func Ones[T math.Number](size Size) *Matrix[T] {
    12  	matrix := New[T](size)
    13  
    14  	for i := range size.Elements() {
    15  		matrix.values[i] = 1
    16  	}
    17  
    18  	return matrix
    19  }
    20  
    21  // Identity returns an identity matrix of the given size.
    22  // Identity matrix is a square matrix with ones on the main diagonal and zeros elsewhere.
    23  func Identity[T math.Number](size Size) *Matrix[T] {
    24  	matrix := Zeros[T](size)
    25  
    26  	for i := range min(size.Height, size.Width) {
    27  		matrix.Set(i, i, 1)
    28  	}
    29  
    30  	return matrix
    31  }