Dot product: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(12 intermediate revisions by 7 users not shown)
Line 276:
end.
</syntaxhighlight>
 
=={{header|Amazing Hopper}}==
Version 1:
<syntaxhighlight lang="c">
#include <basico.h>
 
principal {
imprimir(producto punto( lst'1,3,(-5)', lst'4,(-2),(-1)' ),NL)
terminar
}
</syntaxhighlight>
{{out}}
<pre>
3.00000
</pre>
Version 2:
<syntaxhighlight lang="c">
#define maincode main: {1}do
#define this {1}do
#defn out {"\n"}print
#define dotp mul;stats(0)
#defn lst(*) {"\033"} *;mklist;
#define ready {0}return
#define decim _X_DECIM=0, mov(_X_DECIM),prec(_X_DECIM),{1}do
 
main code{
{0}decim{
"A.B = "
this{
lst (1,3,(-5)), lst (4,(-2),(-1))
} dotp
} out
} ready
</syntaxhighlight>
{{out}}
<pre>
A.B = 3
</pre>
Version 3:
<syntaxhighlight lang="c">
#defn dotp(_X_,_Y_) #ATOM#CMPLX;#ATOM#CMPLX; mul; stats(0)
#defn lst(*) {"\033"} *;mklist;
#defn out(*) *;{"\n"}print
#defn code(*) main:; *; {"0"};return
 
code( out( dotp( lst (1,3,(-5)), lst (4,(-2),(-1)) ) ) )
</syntaxhighlight>
{{out}}
<pre>
3.00000
</pre>
<p>etc...</p>
 
=={{header|APL}}==
Line 864 ⟶ 916:
{{out}}
<pre>3</pre>
 
=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">dim a[1, 3, -5]
dim b[4, -2, -1]
 
arraysize n, a
 
for i = 0 to n - 1
 
let s = s + a[i] * b[i]
 
next i
 
print s</syntaxhighlight>
{{out| Output}}<pre>3</pre>
 
=={{header|Crystal}}==
Line 1,012 ⟶ 1,079:
{{out}}
<pre>3</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
func dotprod a[] b[] .
for i to len a[]
r += a[i] * b[i]
.
return r
.
print dotprod [ 1 3 -5 ] [ 4 -2 -1 ]
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 1,364 ⟶ 1,442:
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Dot_product}}
 
'''Solution'''
 
Dot product is intrinsically supported in Fōrmulæ.
 
'''Test case'''
 
[[File:Fōrmulæ - Dot product 01.png]]
 
[[File:Fōrmulæ - Dot product 02.png]]
 
'''Special cases'''
 
[[File:Fōrmulæ - Dot product 03.png]]
 
[[File:Fōrmulæ - Dot product 04.png]]
 
[[File:Fōrmulæ - Dot product 05.png]]
 
[[File:Fōrmulæ - Dot product 06.png]]
 
'''Programmed.''' A program can be created to calculate the dot product of two vectors:
 
[[File:Fōrmulæ - Dot product 07.png]]
 
=={{header|GAP}}==
Line 2,816 ⟶ 2,918:
 
=={{header|REXX}}==
===noWith error checking===
<syntaxhighlight lang="rexx">/*REXX program computes the dot product of two equal size vectors (of any size).*/
vectorA = ' 1 3 -5 ' vectorA = ' 1 3 -5 ' /*populate vector A with some numbers*/
vectorB = ' 4 -2 -1 ' vectorB = ' 4 -2 -1 ' /* " " B " " " */
saySay 'vector A = ' vectorA /*display the elements in theof vector A. */
saySay 'vector B = ' vectorB /* " " " " " "B. B.*/
p=.Proddot_product(vectorA, vectorB) /*invoke function & compute dot product*/
say Say /*display a blank line for readability.*/
say Say 'dot product = ' p /*display the dot product to terminal. */
exit Exit /*stick a fork in it, we're all done. */
/*------------------------------------------------------------------------------*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
.Proddot_product: procedure; parse arg A,B /*this function compute the dot product */
Parse Arg A,B
$=0 /*initialize the sum to 0 (zero). */
/* Begin Error Checking do j=1 for words(A) /*multiply each number in the vectors. */
If words(A)<>words(B) Then
$=$+word(A,j) * word(B,j) /* ··· and add the product to the sum.*/
Call exit 'Vectors aren''t the same size:' words(A) '<>' words(B)
end /*j*/
Do i=1 To words(A)
return $ /*return the sum to function's invoker.*/</syntaxhighlight>
If datatype(word(A,i))<>'NUM' Then
Call exit 'Element' i 'of vector A isn''t a number:' word(A,i)
If datatype(word(B,i))<>'NUM' Then
Call exit 'Element' i 'of vector B isn''t a number:' word(B,i)
End
/* End Error Checking */
product=0 /* initialize the sum to 0 (zero).*/
Do i=1 To words(A)
product=product+word(A,i)*word(B,i) /*multiply corresponding numbers */
End
Return product
exit:
Say '***error***' arg(1)
Exit 13
</syntaxhighlight>
'''output''' &nbsp; using the default (internal) inputs:
<pre>
Line 2,838 ⟶ 2,955:
vector B = 4 -2 -1
 
dot product = 3</pre>
</pre>
 
===with error checking===
<syntaxhighlight lang="rexx">/*REXX program computes the dot product of two equal size vectors (of any size).*/
vectorA = ' 1 3 -5 ' /*populate vector A with some numbers*/
vectorB = ' 4 -2 -1 ' /* " " B " " " */
say 'vector A = ' vectorA /*display the elements in the vector A.*/
say 'vector B = ' vectorB /* " " " " " " B.*/
p=.prod(vectorA, vectorB) /*invoke function & compute dot product*/
say /*display a blank line for readability.*/
say 'dot product = ' p /*display the dot product to terminal. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
.prod: procedure; parse arg A,B /*this function compute the dot product*/
lenA = words(A); @.1= 'A' /*the number of numbers in vector A. */
lenB = words(B); @.2= 'B' /* " " " " " " B. */
/*Also, define 2 literals to hold names*/
if lenA\==lenB then do; say "***error*** vectors aren't the same size:" /*oops*/
say ' vector A length = ' lenA
say ' vector B length = ' lenB
exit 13 /*exit pgm with bad─boy return code 13.*/
end
$=0 /*initialize the sum to 0 (zero). */
do j=1 for lenA /*multiply each number in the vectors. */
#.1=word(A,j) /*use array to hold 2 numbers at a time*/
#.2=word(B,j)
do k=1 for 2; if datatype(#.k,'N') then iterate
say "***error*** vector " @.k ' element' j,
" isn't numeric: " n.k; exit 13
end /*k*/
$=$ + #.1 * #.2 /* ··· and add the product to the sum.*/
end /*j*/
return $ /*return the sum to function's invoker.*/</syntaxhighlight>
'''output''' &nbsp; is the same as the 1<sup>st</sup> REXX version. <br><br>
 
=={{header|Ring}}==
Line 2,901 ⟶ 2,984:
=={{header|RPL}}==
Being a language for a calculator, RPL makes this easy.
<syntaxhighlight lang="rpl"><<
[ 1 3 -5 ]
[ 4 -2 -1 ]
DOT
>></syntaxhighlight>
 
=={{header|Ruby}}==
Line 3,462 ⟶ 3,543:
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn dot(x []int, y []int) ?int {
fn dot(x []int, y []int) !int {
if x.len != y.len {
return error("incompatible lengths")
Line 3,474 ⟶ 3,556:
fn main() {
d := dot([1, 3, -5], [4, -2, -1])?!
 
println(d)
}
}</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 3,493 ⟶ 3,576:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">class Vector {
construct new(a) {
if (a.type != List || a.count == 0 || !a.all { |i| i is Num }) {
Line 3,528 ⟶ 3,611:
{{libheader|Wren-vector}}
Alternatively, using the above module:
<syntaxhighlight lang="ecmascriptwren">import "./vector" for Vector3
 
var v1 = Vector3.new(1, 3, -5)
9,485

edits