From c59d125110ff701e6d4435c76a76b0c83a608071 Mon Sep 17 00:00:00 2001 From: Ojasva Jain <44553464+ojasva@users.noreply.github.com> Date: Mon, 25 Oct 2021 10:57:21 +0530 Subject: [PATCH] Add Sparcity of a Matrix [Hacktoberfest] (#2659) --- Misc/Sparcity.java | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Misc/Sparcity.java diff --git a/Misc/Sparcity.java b/Misc/Sparcity.java new file mode 100644 index 000000000..a14dc05e8 --- /dev/null +++ b/Misc/Sparcity.java @@ -0,0 +1,47 @@ +package Misc; +import java.util.*; +/* +*A matrix is sparse if many of its coefficients are zero (In general if 2/3rd of matrix elements are 0, it is considered as sparse). +*The interest in sparsity arises because its exploitation can lead to enormous computational savings and because many large matrix problems that occur in practice are sparse. +* +* @author Ojasva Jain +*/ + +class Sparcity{ + /* + * @return Sparcity of matrix + * + * where sparcity = number of zeroes/total elements in matrix + * + */ + static double sparcity(double [][] mat){ + int zero =0; + //Traversing the matrix to count number of zeroes + for(int i=0;i