A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
3
Task/Dot-product/FALSE/dot-product.false
Normal file
3
Task/Dot-product/FALSE/dot-product.false
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[[\1-$0=~][$d;2*1+\-ø\$d;2+\-ø@*@+]#]p:
|
||||
3d: {Vectors' length}
|
||||
1 3 5_ 4 2_ 1_ d;$1+ø@*p;!%. {Output: 3}
|
||||
5
Task/Dot-product/Factor/dot-product.factor
Normal file
5
Task/Dot-product/Factor/dot-product.factor
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
USING: kernel math.vectors sequences ;
|
||||
|
||||
: dot-product ( u v -- w )
|
||||
2dup [ length ] bi@ =
|
||||
[ v. ] [ "Vector lengths must be equal" throw ] if ;
|
||||
20
Task/Dot-product/Fantom/dot-product.fantom
Normal file
20
Task/Dot-product/Fantom/dot-product.fantom
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
class DotProduct
|
||||
{
|
||||
static Int dotProduct (Int[] a, Int[] b)
|
||||
{
|
||||
Int result := 0
|
||||
[a.size,b.size].min.times |i|
|
||||
{
|
||||
result += a[i] * b[i]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
public static Void main ()
|
||||
{
|
||||
Int[] x := [1,2,3,4]
|
||||
Int[] y := [2,3,4]
|
||||
|
||||
echo ("Dot product of $x and $y is ${dotProduct(x, y)}")
|
||||
}
|
||||
}
|
||||
4
Task/Dot-product/GAP/dot-product.gap
Normal file
4
Task/Dot-product/GAP/dot-product.gap
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# Built-in
|
||||
|
||||
[1, 3, -5]*[4, -2, -1];
|
||||
# 3
|
||||
4
Task/Dot-product/Groovy/dot-product-1.groovy
Normal file
4
Task/Dot-product/Groovy/dot-product-1.groovy
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
def dotProduct = { x, y ->
|
||||
assert x && y && x.size() == y.size()
|
||||
[x, y].transpose().collect{ it[0] * it[1] }.sum()
|
||||
}
|
||||
1
Task/Dot-product/Groovy/dot-product-2.groovy
Normal file
1
Task/Dot-product/Groovy/dot-product-2.groovy
Normal file
|
|
@ -0,0 +1 @@
|
|||
println dotProduct([1, 3, -5], [4, -2, -1])
|
||||
9
Task/Dot-product/Icon/dot-product.icon
Normal file
9
Task/Dot-product/Icon/dot-product.icon
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
procedure main()
|
||||
write("a dot b := ",dotproduct([1, 3, -5],[4, -2, -1]))
|
||||
end
|
||||
|
||||
procedure dotproduct(a,b) #: return dot product of vectors a & b or error
|
||||
if *a ~= *b & type(a) == type(b) == "list" then runerr(205,a) # invalid value
|
||||
every (dp := 0) +:= a[i := 1 to *a] * b[i]
|
||||
return dp
|
||||
end
|
||||
5
Task/Dot-product/J/dot-product.j
Normal file
5
Task/Dot-product/J/dot-product.j
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
1 3 _5 +/ . * 4 _2 _1
|
||||
3
|
||||
dotp=: +/ . * NB. Or defined as a verb (function)
|
||||
1 3 _5 dotp 4 _2 _1
|
||||
3
|
||||
3
Task/Dot-product/Julia/dot-product.julia
Normal file
3
Task/Dot-product/Julia/dot-product.julia
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
x = [1 3 -5]
|
||||
y = [4 -2 -1]
|
||||
z = dot(x, y)
|
||||
5
Task/Dot-product/K/dot-product.k
Normal file
5
Task/Dot-product/K/dot-product.k
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
+/1 3 -5 * 4 -2 -1
|
||||
3
|
||||
|
||||
1 3 -5 _dot 4 -2 -1
|
||||
3
|
||||
24
Task/Dot-product/Liberty-BASIC/dot-product.liberty
Normal file
24
Task/Dot-product/Liberty-BASIC/dot-product.liberty
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
vectorA$ = "1, 3, -5"
|
||||
vectorB$ = "4, -2, -1"
|
||||
print "DotProduct of ";vectorA$;" and "; vectorB$;" is ";
|
||||
print DotProduct(vectorA$, vectorB$)
|
||||
|
||||
'arbitrary length
|
||||
vectorA$ = "3, 14, 15, 9, 26"
|
||||
vectorB$ = "2, 71, 18, 28, 1"
|
||||
print "DotProduct of ";vectorA$;" and "; vectorB$;" is ";
|
||||
print DotProduct(vectorA$, vectorB$)
|
||||
|
||||
end
|
||||
|
||||
function DotProduct(a$, b$)
|
||||
DotProduct = 0
|
||||
i = 1
|
||||
while 1
|
||||
x$=word$( a$, i, ",")
|
||||
y$=word$( b$, i, ",")
|
||||
if x$="" or y$="" then exit function
|
||||
DotProduct = DotProduct + val(x$)*val(y$)
|
||||
i = i+1
|
||||
wend
|
||||
end function
|
||||
5
Task/Dot-product/Logo/dot-product.logo
Normal file
5
Task/Dot-product/Logo/dot-product.logo
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
to dotprod :a :b
|
||||
output apply "sum (map "product :a :b)
|
||||
end
|
||||
|
||||
show dotprod [1 3 -5] [4 -2 -1] ; 3
|
||||
7
Task/Dot-product/Logtalk/dot-product.logtalk
Normal file
7
Task/Dot-product/Logtalk/dot-product.logtalk
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
dot_product(A, B, Sum) :-
|
||||
dot_product(A, B, 0, Sum).
|
||||
|
||||
dot_product([], [], Sum, Sum).
|
||||
dot_product([A| As], [B| Bs], Acc, Sum) :-
|
||||
Acc2 is Acc + A*B,
|
||||
dot_product(As, Bs, Acc2, Sum).
|
||||
9
Task/Dot-product/MUMPS/dot-product.mumps
Normal file
9
Task/Dot-product/MUMPS/dot-product.mumps
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
DOTPROD(A,B)
|
||||
;Returns the dot product of two vectors. Vectors are assumed to be stored as caret-delimited strings of numbers.
|
||||
;If the vectors are not of equal length, a null string is returned.
|
||||
QUIT:$LENGTH(A,"^")'=$LENGTH(B,"^") ""
|
||||
NEW I,SUM
|
||||
SET SUM=0
|
||||
FOR I=1:1:$LENGTH(A,"^") SET SUM=SUM+($PIECE(A,"^",I)*$PIECE(B,"^",I))
|
||||
KILL I
|
||||
QUIT SUM
|
||||
1
Task/Dot-product/Mathematica/dot-product.mathematica
Normal file
1
Task/Dot-product/Mathematica/dot-product.mathematica
Normal file
|
|
@ -0,0 +1 @@
|
|||
{1,3,-5}.{4,-2,-1}
|
||||
2
Task/Dot-product/Maxima/dot-product.maxima
Normal file
2
Task/Dot-product/Maxima/dot-product.maxima
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
[1, 3, -5] . [4, -2, -1];
|
||||
/* 3 */
|
||||
17
Task/Dot-product/Mercury/dot-product.mercury
Normal file
17
Task/Dot-product/Mercury/dot-product.mercury
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
:- module dot_product.
|
||||
:- interface.
|
||||
|
||||
:- import_module io.
|
||||
:- pred main(io::di, io::uo) is det.
|
||||
|
||||
:- implementation.
|
||||
:- import_module int, list.
|
||||
|
||||
main(!IO) :-
|
||||
io.write_int([1, 3, -5] `dot_product` [4, -2, -1], !IO),
|
||||
io.nl(!IO).
|
||||
|
||||
:- func dot_product(list(int), list(int)) = int.
|
||||
|
||||
dot_product(As, Bs) =
|
||||
list.foldl_corresponding((func(A, B, Acc) = Acc + A * B), As, Bs, 0).
|
||||
Loading…
Add table
Add a link
Reference in a new issue