Matrix multiplication: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: fixed up a comment. -- ~~~~) |
(→{{header|Go}}: added library based solution) |
||
Line 1,120: | Line 1,120: | ||
<lang go>package main |
<lang go>package main |
||
import |
import "fmt" |
||
⚫ | |||
⚫ | |||
type Value float64 |
type Value float64 |
||
Line 1,174: | Line 1,172: | ||
fmt.Printf("Product of A and B:\n%s\n\n", P) |
fmt.Printf("Product of A and B:\n%s\n\n", P) |
||
}</lang> |
}</lang> |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
Matrix A: |
Matrix A: |
||
Line 1,285: | Line 1,283: | ||
}</lang> |
}</lang> |
||
Output is similar to 2D version. |
Output is similar to 2D version. |
||
===Library=== |
|||
<lang go>package main |
|||
import ( |
|||
⚫ | |||
mat "github.com/skelterjohn/go.matrix" |
|||
⚫ | |||
func main() { |
|||
a := mat.MakeDenseMatrixStacked([][]float64{ |
|||
{1, 1, 1, 1}, |
|||
{2, 4, 8, 16}, |
|||
{3, 9, 27, 81}, |
|||
{4, 16, 64, 256}, |
|||
}) |
|||
b := mat.MakeDenseMatrixStacked([][]float64{ |
|||
{ |
|||
4, |
|||
-3, |
|||
4. / 3, |
|||
-1. / 4, |
|||
}, |
|||
{ |
|||
-13. / 3, |
|||
19. / 4, |
|||
-7. / 3, |
|||
11. / 24, |
|||
}, |
|||
{ |
|||
3. / 2, |
|||
-2, |
|||
7. / 6, |
|||
-1. / 4, |
|||
}, |
|||
{ |
|||
-1. / 6, |
|||
1. / 4, |
|||
-1. / 6, |
|||
1. / 24, |
|||
}, |
|||
}) |
|||
p, err := a.TimesDense(b) |
|||
fmt.Printf("Matrix A:\n%v\n", a) |
|||
fmt.Printf("Matrix B:\n%v\n", b) |
|||
if err != nil { |
|||
fmt.Println(err) |
|||
return |
|||
} |
|||
fmt.Printf("Product of A and B:\n%v\n", p) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Matrix A: |
|||
{ 1, 1, 1, 1, |
|||
2, 4, 8, 16, |
|||
3, 9, 27, 81, |
|||
4, 16, 64, 256} |
|||
Matrix B: |
|||
{ 4, -3, 1.333333, -0.25, |
|||
-4.333333, 4.75, -2.333333, 0.458333, |
|||
1.5, -2, 1.166667, -0.25, |
|||
-0.166667, 0.25, -0.166667, 0.041667} |
|||
Product of A and B: |
|||
{ 1, 0, -0, -0, |
|||
0, 1, -0, -0, |
|||
0, 0, 1, 0, |
|||
0, 0, 0, 1} |
|||
</pre> |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |