Dot product: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
m (→JS ES6: Tidied) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(19 intermediate revisions by 11 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,363 ⟶ 1,441:
=={{header|Fōrmulæ}}==
{{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 1,590 ⟶ 1,688:
===ES6===
Composing functional primitives into a '''dotProduct()''' which returns a '''
<syntaxhighlight lang="javascript">(() => {
Line 1,597 ⟶ 1,695:
// ------------------- DOT PRODUCT -------------------
// dotProduct :: [
const dotProduct = xs =>
ys => xs.length === ys.length
? sum(zipWith(mul)(xs)(ys))
:
Line 1,911 ⟶ 2,009:
=={{header|M2000 Interpreter}}==
Version 12 can use types for arrays (earlier versions use variant type by default).
So we can adjust the return value of Dot() to be the same as the first item of first array. All functions of M2000 return variant type (including objects) or array of variant values, for multiple values (which is an object too).
<syntaxhighlight lang="m2000 interpreter">
Module dot_product {
Line 1,918 ⟶ 2,020:
if len(a)<>len(b) Then Error "not same length"
if len(a)=0 then Error "empty vectors"
// take type by first item in a()
long lowbound=dimension(a,1,0)
sum=a#val(lowbound)-a#val(lowbound)
While a1, b1 {sum+=array(a1)*array(b1)}
=sum
}
Print Dot(A, B)=3
Print Dot((1,3,-5), (4,-2,-1), 0)=3
dim k(2 to 4) as long long, z(3) as long long
k(2)=1,3,-5
z(0)=4,-2,-1
result=Dot(k(), z())
Print result=3, type$(result)="Long Long"
}
Module dot_product
Line 2,325 ⟶ 2,435:
sum(i=1,#u,u[i]*v[i])
};</syntaxhighlight>
===Alternative===
<syntaxhighlight lang="parigp">dot(u,v) = u * v~;</syntaxhighlight>
=={{header|Pascal}}==
Line 2,805 ⟶ 2,918:
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX program
vectorA = ' 1 3 -5 '
vectorB = ' 4 -2 -1 '
p=
/*------------------------------------------------------------------------------*/
Parse Arg A,B
/* Begin Error Checking
If words(A)<>words(B) Then
Call exit 'Vectors aren''t the same size:' words(A) '<>' words(B)
Do i=1 To words(A)
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''' using the default (internal) inputs:
<pre>
Line 2,827 ⟶ 2,955:
vector B = 4 -2 -1
dot product = 3</pre>
=={{header|Ring}}==
Line 2,890 ⟶ 2,984:
=={{header|RPL}}==
Being a language for a calculator, RPL makes this easy.
[ 1 3 -5 ]
[ 4 -2 -1 ]
DOT
=={{header|Ruby}}==
Line 3,451 ⟶ 3,543:
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">
fn dot(x []int, y []int) !int {
if x.len != y.len {
return error("incompatible lengths")
Line 3,463 ⟶ 3,556:
fn main() {
d := dot([1, 3, -5], [4, -2, -1])
println(d)
}
</syntaxhighlight>
{{out}}
Line 3,482 ⟶ 3,576:
=={{header|Wren}}==
<syntaxhighlight lang="
construct new(a) {
if (a.type != List || a.count == 0 || !a.all { |i| i is Num }) {
Line 3,513 ⟶ 3,607:
<pre>
The dot product of [1, 3, -5] and [4, -2, -1] is 3.
</pre>
{{libheader|Wren-vector}}
Alternatively, using the above module:
<syntaxhighlight lang="wren">import "./vector" for Vector3
var v1 = Vector3.new(1, 3, -5)
var v2 = Vector3.new(4, -2, -1)
System.print("The dot product of %(v1) and %(v2) is %(v1.dot(v2)).")</syntaxhighlight>
{{out}}
<pre>
The dot product of (1, 3, -5) and (4, -2, -1) is 3.
</pre>
|