Skip to content

Add matrix multiplication with double[][] and unit tests #6417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
fcfd803
MatrixMultiplication.java created and updated.
Nishitha0730 Jul 19, 2025
c9f57d0
Add necessary comment to MatrixMultiplication.java
Nishitha0730 Jul 19, 2025
16c9897
Create MatrixMultiplicationTest.java
Nishitha0730 Jul 19, 2025
1ff92f5
method for 2 by 2 matrix multiplication is created
Nishitha0730 Jul 19, 2025
855d20f
Use assertMatrixEquals(), otherwise there can be error due to floatin…
Nishitha0730 Jul 19, 2025
05064a5
assertMatrixEquals method created and updated
Nishitha0730 Jul 19, 2025
4e4625c
method created for 3by2 matrix multiply with 2by1 matrix
Nishitha0730 Jul 19, 2025
f661455
method created for null matrix multiplication
Nishitha0730 Jul 19, 2025
a9438d2
method for test matrix dimension error
Nishitha0730 Jul 19, 2025
4bf395b
method for test empty matrix input
Nishitha0730 Jul 19, 2025
64a97db
testMultiply3by2and2by1 test case updated
Nishitha0730 Jul 19, 2025
6d16a54
Check for empty matrices part updated
Nishitha0730 Jul 19, 2025
65927d8
Updated Unit test coverage
Nishitha0730 Jul 19, 2025
1a58232
files updated
Nishitha0730 Jul 19, 2025
414200e
clean the code
Nishitha0730 Jul 19, 2025
d150bb2
clean the code
Nishitha0730 Jul 19, 2025
1858c69
Updated files with google-java-format
Nishitha0730 Jul 19, 2025
56baf84
Updated files
Nishitha0730 Jul 19, 2025
ca40a2b
Updated files
Nishitha0730 Jul 19, 2025
d633a3e
Updated files
Nishitha0730 Jul 19, 2025
4257686
Updated files
Nishitha0730 Jul 19, 2025
a9d9c39
Merge branch 'master' into my_algorithm
DenizAltunkapan Jul 19, 2025
dcec054
Add reference links and complexities
Nishitha0730 Jul 19, 2025
410d22f
Merge remote-tracking branch 'origin/my_algorithm' into my_algorithm
Nishitha0730 Jul 19, 2025
c243140
Add test cases for 1by1 matrix and non-rectangular matrix
Nishitha0730 Jul 19, 2025
49d7f98
Add reference links and complexities
Nishitha0730 Jul 19, 2025
157d6c8
Merge branch 'master' into my_algorithm
Nishitha0730 Jul 20, 2025
5328d9a
Merge branch 'master' into my_algorithm
Nishitha0730 Jul 20, 2025
8c1beaa
Merge branch 'master' into my_algorithm
DenizAltunkapan Jul 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updated files
  • Loading branch information
Nishitha0730 committed Jul 19, 2025
commit ca40a2b1d2e1d8e0a120d74889aeec34a8c27c7c
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ public static double[][] multiply(double[][] matrixA, double[][] matrixB) {
}

// Check for empty matrices
if (matrixA.length == 0
|| matrixB.length == 0
|| matrixA[0].length == 0
|| matrixB[0].length == 0) {
if (matrixA.length == 0 || matrixB.length == 0 || matrixA[0].length == 0 || matrixB[0].length == 0) {
throw new IllegalArgumentException("Input matrices must not be empty");
}
}

// Validate the matrix dimensions
if (matrixA[0].length != matrixB.length) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.thealgorithms.matrix;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;


public class MatrixMultiplicationTest {

private static final double EPSILON = 1e-9; // for floating point comparison
Expand Down Expand Up @@ -32,24 +34,21 @@ void testMultiply3by2and2by1() {
@Test
void testNullMatrixA() {
double[][] b = {{1, 2}, {3, 4}};
assertThrows(IllegalArgumentException.class,
() -> MatrixMultiplication.multiply(null, b));
assertThrows(IllegalArgumentException.class, () -> MatrixMultiplication.multiply(null, b));
}

@Test
void testNullMatrixB() {
double[][] a = {{1, 2}, {3, 4}};
assertThrows(IllegalArgumentException.class,
() -> MatrixMultiplication.multiply(a, null));
assertThrows(IllegalArgumentException.class, () -> MatrixMultiplication.multiply(a, null));
}

@Test
void testMultiplyNull() {
double[][] matrixA = {{1.0, 2.0}, {3.0, 4.0}};
double[][] matrixB = null;

Exception exception = assertThrows(IllegalArgumentException.class,
() -> MatrixMultiplication.multiply(matrixA, matrixB));
Exception exception = assertThrows(IllegalArgumentException.class, () -> MatrixMultiplication.multiply(matrixA, matrixB));

String expectedMessage = "Input matrices cannot be null";
String actualMessage = exception.getMessage();
Expand All @@ -61,16 +60,14 @@ void testMultiplyNull() {
void testIncompatibleDimensions() {
double[][] a = {{1.0, 2.0}};
double[][] b = {{1.0, 2.0}};
assertThrows(IllegalArgumentException.class,
() -> MatrixMultiplication.multiply(a, b));
assertThrows(IllegalArgumentException.class, () -> MatrixMultiplication.multiply(a, b));
}

@Test
void testEmptyMatrices() {
double[][] a = new double[0][0];
double[][] b = new double[0][0];
assertThrows(IllegalArgumentException.class,
() -> MatrixMultiplication.multiply(a, b));
assertThrows(IllegalArgumentException.class, () -> MatrixMultiplication.multiply(a, b));
}

private void assertMatrixEquals(double[][] expected, double[][] actual) {
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy