-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
63 lines (56 loc) · 1.25 KB
/
Matrix.java
File metadata and controls
63 lines (56 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Practice 1.1.33. Create a Matrix library to implement vector dot, product(Mat-Mat, Vec-Mat, Mat-Vec, through overload), transpose,
import edu.princeton.cs.algs4.StdOut;
public class Matrix{
public static double[] dot(double[] a, double[] b)
{
double[] c = new double[a.length];
if(a.length!=b.length)
{
StdOut.println("Vector sizes must match!");
return c;
}
if(a.length==0 || b.length==0)
{
StdOut.println("Vector is empty!");
return c;
}
for(int i=0;i<a.length;i++)
{
c[i] = a[i] * b[i];
}
return c;
}
public static double[][] mult(double[][] a, double[][] b)
{
double[][] c = new double[a.length][a.length];
if(a.length==0 || b.length==0)
{
StdOut.println("Error: Vector is empty!");
return c;
}
if(a.length!=a[0].length || b.length!=b[0].length)
{
StdOut.println("Error: Matrix must be square");
return c;
}
if(a.length!=b.length)
{
StdOut.println("Vector sizes must match!");
return c;
}
int size = a.length;
for(int i=0;i<size;i++)
for(int j=0;j<size;j++)
for(int k=0;k<size;k++)
{
c[i][j] += a[i][k] * b[k][j];
}
return c;
}
public static void main(String[] args)
{
int[][] c = new int[3][4];
int d = c[1].length;
StdOut.println(d);
}
}