Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
exercises
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ISC2
oop
exercises
Commits
8fefa681
Commit
8fefa681
authored
1 year ago
by
iliya
Browse files
Options
Downloads
Patches
Plain Diff
matrix
parent
74ea09e9
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
serie1/Matrix.java
+56
-0
56 additions, 0 deletions
serie1/Matrix.java
with
56 additions
and
0 deletions
serie1/Matrix.java
0 → 100644
+
56
−
0
View file @
8fefa681
public
class
Matrix
{
public
static
void
printMatrix
(
int
[][]
mat
)
{
for
(
int
i
=
0
;
i
<
mat
.
length
;
i
++)
{
for
(
int
j
=
0
;
j
<
mat
[
i
].
length
;
j
++)
{
System
.
out
.
printf
(
"%d "
,
mat
[
i
][
j
]);
}
System
.
out
.
println
();
}
}
public
static
int
[][]
mul
(
int
[][]
lhs
,
int
[][]
rhs
)
{
if
(
lhs
[
0
].
length
!=
rhs
.
length
)
{
throw
new
RuntimeException
(
"y axis of lhs doesn't match the x axis of rhs, multiplication cannot be done"
);
}
int
[][]
res
=
new
int
[
lhs
.
length
][
rhs
[
0
].
length
];
for
(
int
i
=
0
;
i
<
lhs
.
length
;
i
++)
{
for
(
int
j
=
0
;
j
<
rhs
[
i
].
length
;
j
++)
{
for
(
int
k
=
0
;
k
<
rhs
.
length
;
k
++)
{
res
[
i
][
j
]
+=
lhs
[
i
][
k
]
*
rhs
[
k
][
j
];
}
}
}
return
res
;
}
public
int
[][]
add
(
int
[][]
lhs
,
int
[][]
rhs
)
{
if
(
lhs
.
length
!=
rhs
.
length
||
lhs
[
0
].
length
!=
rhs
[
0
].
length
)
{
throw
new
RuntimeException
(
"y axis of lhs doesn't match the x axis of rhs, multiplication cannot be done"
);
}
int
[][]
res
=
new
int
[
lhs
.
length
][
lhs
[
0
].
length
];
for
(
int
i
=
0
;
i
<
res
.
length
;
i
++)
{
for
(
int
j
=
0
;
j
<
res
[
i
].
length
;
j
++)
{
res
[
i
][
j
]
=
lhs
[
i
][
j
]
+
rhs
[
i
][
j
];
}
}
return
res
;
}
public
static
void
main
(
String
[]
args
)
{
int
[][]
mat1
=
new
int
[
3
][
5
];
int
[][]
mat2
=
new
int
[
5
][
4
];
int
res
[][]
=
mul
(
mat1
,
mat2
);
printMatrix
(
res
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment