-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMatrix Class
More file actions
153 lines (135 loc) · 2.86 KB
/
Matrix Class
File metadata and controls
153 lines (135 loc) · 2.86 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
Matrix Class
Send Feedback
Write a code to perform different operations on matrix.
1. Addition
You are given two matrices return the addition of these two matrices.
2. Multiplication
Given two matrices return the matrix multiplication of them.(Both the matrices will always be multiplicable).
3. Transpose
Given a Matrix calculate the transpose of the matrix and return it. (Number of rows and columns will be same)
4. Rotate by 90
Given a Matrix, rotate the matrix by 90 degree in anticlockwise direction.
Input format :
Line 1: Operation to be performed
Line 2 : No of rows(n1) & No of columns(m1) (separated by space)
Line 3 : Row 1 elements (separated by space)
Line 4 : Row 2 elements (separated by space)
Line 5 : and so on
(If needed)
Line n1+2 : No of rows(n2) & No of columns(m2)(separated by space)
Line n1+3 : Row 1 elements (separated by space)
Line n1+4 : Row 2 elements (separated by space)
Line n1+5 : and so on
Sample Input 1 :
1
2 2
1 2
1 3
2 2
4 3
1 5
Sample output 1 :
5 5
2 8
Sample Input 2 :
2
2 2
1 2
1 3
2 2
4 3
1 5
Sample output 2 :
6 13
7 18
Sample Input 3 :
3
2 2
1 2
1 3
Sample output 3 :
1 1
2 3
Sample Input 4 :
4
2 2
1 2
1 3
Sample output 4 :
2 3
1 1
code in java ********************************************************
public class mat
{
int[][] matrix;
mat (int[][]mat)
{
matrix = mat;
}
public static mat add (mat a, mat b)
{
int[][] data = new int[a.matrix.length][a.matrix[0].length];
mat c = new mat (data);
for (int i = 0; i < a.matrix.length; i++)
{
for (int j = 0; j < a.matrix[0].length; j++)
{
c.matrix[i][j] = a.matrix[i][j] + b.matrix[i][j];
}
}
return c;
}
public static mat multiply (mat a, mat b)
{
int[][] data = new int[a.matrix.length][a.matrix.length];
mat c = new mat (data);
for (int i = 0; i < a.matrix.length; i++)
{
for (int j = 0; j < b.matrix[0].length; j++)
{
for (int k = 0; k < a.matrix[0].length; k++)
{
c.matrix[i][j] += a.matrix[i][k] * b.matrix[k][j];
}
}
}
return c;
}
public static mat transpose (mat m)
{
int[][] data = new int[m.matrix.length][m.matrix[0].length];
mat c = new mat (data);
for (int i = 0; i < m.matrix.length; i++)
{
for (int j = 0; j < m.matrix[0].length; j++)
{
c.matrix[i][j] = m.matrix[j][i];
}
}
return c;
}
public static mat rotate (mat m)
{
int[][] data = new int[m.matrix.length][m.matrix[0].length];
mat c = new mat (data);
for (int i = 0; i < m.matrix.length; i++)
{
for (int j = 0; j < m.matrix[0].length; j++)
{
c.matrix[i][j] = m.matrix[j][m.matrix.length - 1 - i];
}
}
return c;
}
public void print ()
{
for (int i = 0; i < matrix.length; i++)
{
for (int j = 0; j < matrix[0].length; j++)
{
System.out.print (matrix[i][j] + " ");
}
System.out.println ();
}
}
}