Code Monkey home page Code Monkey logo

basemax / sparsematrixlinkedlistgo Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 23 KB

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.

License: GNU General Public License v3.0

Go 100.00%
data-structure datastructure ds go golang linkedlist sparse sparse-matrix sparsematrix

sparsematrixlinkedlistgo's Introduction

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 matrix
type Node struct {
	row, col int
	value    float64
	next     *Node
}

// Storing a sparse matrix
type SparseMatrix struct {
	head       *Node
	rows, cols int
}

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"

func main() {
	// Create a new sparse matrix
	matrix := NewSparseMatrix(3, 3)

	// Add some values
	matrix.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 matrix
	matrix.Print()

	// Get the value at a specific row and column
	fmt.Println(matrix.Get(0, 0))

	// Check if the matrix is empty
	fmt.Println(matrix.IsEmpty())

	// Get the number of rows
	fmt.Println(matrix.Rows())

	// Get the number of columns
	fmt.Println(matrix.Cols())

	// Get the size of the matrix
	fmt.Println(matrix.Size())

	// Check if the matrix has a value at a specific row and column
	fmt.Println(matrix.Has(0, 0))

	// Check if the matrix has a value at a specific index
	fmt.Println(matrix.HasIndex(0))

	// Get the value at a specific index
	fmt.Println(*matrix.GetIndex(0))

	// Create a new sparse matrix
	matrix2 := NewSparseMatrix(3, 3)

	// Add some values
	matrix2.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 matrix
	matrix2.Print()

	// Add two sparse matrices
	matrix3 := matrix.AddMatrix(matrix2)

	// Print the matrix
	matrix3.Print()

	// Subtract two sparse matrices
	matrix4 := matrix.SubMatrix(matrix2)

	// Print the matrix
	matrix4.Print()

	// Multiply two sparse matrices
	matrix5 := matrix.MulMatrix(matrix2)

	// Print the matrix
	matrix5.Print()

	// Divide two sparse matrices
	matrix6 := matrix.DivMatrix(matrix2)

	// Print the matrix
	matrix6.Print()
}

License

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

© Copyright (c) 2022, Max Base

sparsematrixlinkedlistgo's People

Contributors

basemax avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.