Jump to content

Strassen's algorithm: Difference between revisions

m
syntax highlighting fixup automation
(→‎{{header|Wren}}: Added a version which uses Wren-matrix (retaining original as there are translations of it).)
m (syntax highlighting fixup automation)
Line 24:
{{trans|Wren}}
Rather than use a library such as gonum, we create a simple Matrix type which is adequate for this task.
<langsyntaxhighlight lang="go">package main
 
import (
Line 188:
fmt.Printf(" c * d = %v\n", strassen(c, d).toString(6))
fmt.Printf(" e * f = %v\n", strassen(e, f))
}</langsyntaxhighlight>
 
{{out}}
Line 206:
===With dynamic padding===
Because Julia uses column major in matrices, sometimes the code uses the adjoint of a matrix in order to match examples as written.
<langsyntaxhighlight lang="julia">"""
Strassen's matrix multiplication algorithm.
Use dynamic padding in order to reduce required auxiliary memory.
Line 323:
intprint("Regular multiply: ", R * R)
intprint("Strassen multiply: ", strassen(R,R))
</langsyntaxhighlight>{{out}}
<pre>
Regular multiply: [19 43; 22 50]
Line 337:
===Recursive===
Output is the same as the dynamically padded version.
<langsyntaxhighlight Julialang="julia">function Strassen(A, B)
n = size(A, 1)
if n == 1
Line 387:
intprint("Regular multiply: ", R * R)
intprint("Strassen multiply: ", Strassen(R,R))
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
{{trans|Go}}
{{trans|Wren}}
<langsyntaxhighlight Nimlang="nim">import math, sequtils, strutils
 
type Matrix = seq[seq[float]]
Line 531:
echo " a * b = ", strassen(a, b).toString(10)
echo " c * d = ", strassen(c, d).toString(6)
echo " e * f = ", strassen(e, f).toString(10)</langsyntaxhighlight>
 
{{out}}
Line 546:
=={{header|Phix}}==
As noted on wp, you could pad with zeroes, and strip them on exit, instead of crashing for non-square 2<sup><small>n</small></sup> matrices.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">strassen</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
Line 629:
<span style="color: #0000FF;">{-</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">}}</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">strassen</span><span style="color: #0000FF;">(</span><span style="color: #000000;">R</span><span style="color: #0000FF;">,</span><span style="color: #000000;">R</span><span style="color: #0000FF;">))</span>
<!--</langsyntaxhighlight>-->
{{out}}
Matches that of [[Matrix_multiplication#Phix]], when given the same inputs. Note that a few "-0" show up in the second one (the identity matrix) under pwa/p2js.
Line 648:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">"""Matrix multiplication using Strassen's algorithm. Requires Python >= 3.7."""
 
from __future__ import annotations
Line 814:
if __name__ == "__main__":
examples()
</syntaxhighlight>
</lang>
 
{{out}}
Line 831:
Special thanks go to the module author, [https://github.com/frithnanth Fernando Santagata], on showing how to deal with a pass-by-value case.
{{trans|Julia}}
<syntaxhighlight lang="raku" perl6line># 20210126 Raku programming solution
 
use Math::Libgsl::Constants;
Line 924:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1
=end code</langsyntaxhighlight>
{{out}}
<pre>Regular multiply:
Line 955:
=={{header|Swift}}==
[https://github.com/hollance/Matrix/blob/master/Matrix.swift '''Matrix Class'''] by [https://github.com/ozgurshn ozgurshn]
<langsyntaxhighlight lang="swift">
// Matrix Strassen Multiplication
func strassenMultiply(matrix1: Matrix, matrix2: Matrix) -> Matrix {
Line 1,118:
print(result3.description)
}
main()</langsyntaxhighlight>
 
{{out}}
Line 1,144:
 
I've used the Phix entry's examples to test the Strassen algorithm implementation.
<langsyntaxhighlight lang="ecmascript">class Matrix {
construct new(a) {
if (a.type != List || a.count == 0 || a[0].type != List || a[0].count == 0 || a[0][0].type != Num) {
Line 1,297:
System.print(" a * b = %(strassen.call(a, b))")
System.print(" c * d = %(strassen.call(c, d).toString(6))")
System.print(" e * f = %(strassen.call(e, f))")</langsyntaxhighlight>
 
{{out}}
Line 1,314:
{{libheader|Wren-matrix}}
Since the above version was written, a Matrix module has been added and the following version uses it. The output is exactly the same as before.
<langsyntaxhighlight lang="ecmascript">import "./matrix" for Matrix
var params = Fn.new { |r, c|
Line 1,397:
System.print(" a * b = %(strassen.call(a, b))")
System.print(" c * d = %(strassen.call(c, d).toString(6))")
System.print(" e * f = %(strassen.call(e, f))")</langsyntaxhighlight>
10,327

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.