Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

11 Commits

Repository files navigation

Sparse-Matrix Linked-List Go

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.

Structure

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 matrix
  • func (m *SparseMatrix) Get(row, col int) float64: Get the value at a specific row and column
  • func (m *SparseMatrix) IsEmpty() bool: Check if the sparse matrix is empty
  • func (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 matrix
  • func (m *SparseMatrix) Cols() int: Get the number of columns in the sparse matrix
  • func (m *SparseMatrix) Has(row, col int) bool: Has a value at a specific row and column
  • func (m *SparseMatrix) HasIndex(index int) bool: Has an index in the real matrix
  • func (m *SparseMatrix) Print(): Print the sparse matrix
  • func (m *SparseMatrix) GetIndex(index int) *float64: Get the value at a specific index, if not found return nil
  • func (m *SparseMatrix) AddMatrix(m2 *SparseMatrix) *SparseMatrix: Add two sparse matrices
  • func (m *SparseMatrix) SubMatrix(m2 *SparseMatrix) *SparseMatrix: Subtract two sparse matrices
  • func (m *SparseMatrix) MulMatrix(m2 *SparseMatrix) *SparseMatrix: Multiply two sparse matrices
  • func (m *SparseMatrix) DivMatrix(m2 *SparseMatrix) *SparseMatrix: Divide two sparse matrices

Example

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()
}

License

This project is licensed under the GPL-3.0 License - see the LICENSE file for details.

© Copyright (c) 2022, Max Base

About

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.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages