Sparse-Matrix Linked-List Go is a sparse matrix implementation in Go using a linked list. This is a data structure that stores only non-zero values in a matrix. This is useful when you have a lot of zeros in your matrix. This data structure is also useful when you want to perform operations on sparse matrices.
Here, we are going to store the sparse matrix in a normal linked-list.
Types:
// Storing a item in sparse matrixtypeNodestruct {
row, colintvaluefloat64next*Node
}
// Storing a sparse matrixtypeSparseMatrixstruct {
head*Noderows, colsint
}Functions:
func NewSparseMatrix(rows, cols int) *SparseMatrix: Create a new sparse matrix
Methods:
func (m *SparseMatrix) Add(row, col int, value float64): Add a value to the sparse matrixfunc (m *SparseMatrix) Get(row, col int) float64: Get the value at a specific row and columnfunc (m *SparseMatrix) IsEmpty() bool: Check if the sparse matrix is emptyfunc (m *SparseMatrix) Size() (int, int): Get size of the sparse matrix (rows and columns)func (m *SparseMatrix) Rows() int: Get the number of rows in the sparse matrixfunc (m *SparseMatrix) Cols() int: Get the number of columns in the sparse matrixfunc (m *SparseMatrix) Has(row, col int) bool: Has a value at a specific row and columnfunc (m *SparseMatrix) HasIndex(index int) bool: Has an index in the real matrixfunc (m *SparseMatrix) Print(): Print the sparse matrixfunc (m *SparseMatrix) GetIndex(index int) *float64: Get the value at a specific index, if not found return nilfunc (m *SparseMatrix) AddMatrix(m2 *SparseMatrix) *SparseMatrix: Add two sparse matricesfunc (m *SparseMatrix) SubMatrix(m2 *SparseMatrix) *SparseMatrix: Subtract two sparse matricesfunc (m *SparseMatrix) MulMatrix(m2 *SparseMatrix) *SparseMatrix: Multiply two sparse matricesfunc (m *SparseMatrix) DivMatrix(m2 *SparseMatrix) *SparseMatrix: Divide two sparse matrices
package main
import"fmt"funcmain() {
// Create a new sparse matrixmatrix:=NewSparseMatrix(3, 3)
// Add some valuesmatrix.Add(0, 0, 1)
matrix.Add(0, 1, 2)
matrix.Add(0, 2, 3)
matrix.Add(1, 0, 4)
matrix.Add(1, 1, 5)
matrix.Add(1, 2, 6)
matrix.Add(2, 0, 7)
matrix.Add(2, 1, 8)
matrix.Add(2, 2, 9)
// Print the matrixmatrix.Print()
// Get the value at a specific row and columnfmt.Println(matrix.Get(0, 0))
// Check if the matrix is emptyfmt.Println(matrix.IsEmpty())
// Get the number of rowsfmt.Println(matrix.Rows())
// Get the number of columnsfmt.Println(matrix.Cols())
// Get the size of the matrixfmt.Println(matrix.Size())
// Check if the matrix has a value at a specific row and columnfmt.Println(matrix.Has(0, 0))
// Check if the matrix has a value at a specific indexfmt.Println(matrix.HasIndex(0))
// Get the value at a specific indexfmt.Println(*matrix.GetIndex(0))
// Create a new sparse matrixmatrix2:=NewSparseMatrix(3, 3)
// Add some valuesmatrix2.Add(0, 0, 1)
matrix2.Add(0, 1, 2)
matrix2.Add(0, 2, 3)
matrix2.Add(1, 0, 4)
matrix2.Add(1, 1, 5)
matrix2.Add(1, 2, 6)
matrix2.Add(2, 0, 7)
matrix2.Add(2, 1, 8)
matrix2.Add(2, 2, 9)
// Print the matrixmatrix2.Print()
// Add two sparse matricesmatrix3:=matrix.AddMatrix(matrix2)
// Print the matrixmatrix3.Print()
// Subtract two sparse matricesmatrix4:=matrix.SubMatrix(matrix2)
// Print the matrixmatrix4.Print()
// Multiply two sparse matricesmatrix5:=matrix.MulMatrix(matrix2)
// Print the matrixmatrix5.Print()
// Divide two sparse matricesmatrix6:=matrix.DivMatrix(matrix2)
// Print the matrixmatrix6.Print()
}This project is licensed under the GPL-3.0 License - see the LICENSE file for details.
© Copyright (c) 2022, Max Base