This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View 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}

View 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 ;

View 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)}")
}
}

View file

@ -0,0 +1,4 @@
# Built-in
[1, 3, -5]*[4, -2, -1];
# 3

View 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()
}

View file

@ -0,0 +1 @@
println dotProduct([1, 3, -5], [4, -2, -1])

View 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

View 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

View file

@ -0,0 +1,3 @@
x = [1 3 -5]
y = [4 -2 -1]
z = dot(x, y)

View file

@ -0,0 +1,5 @@
+/1 3 -5 * 4 -2 -1
3
1 3 -5 _dot 4 -2 -1
3

View 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

View 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

View 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).

View 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

View file

@ -0,0 +1 @@
{1,3,-5}.{4,-2,-1}

View file

@ -0,0 +1,2 @@
[1, 3, -5] . [4, -2, -1];
/* 3 */

View 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).