September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
21
Task/Dot-product/Aime/dot-product.aime
Normal file
21
Task/Dot-product/Aime/dot-product.aime
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
real
|
||||
dp(list a, list b)
|
||||
{
|
||||
real p, v;
|
||||
integer i;
|
||||
|
||||
p = 0;
|
||||
for (i, v in a) {
|
||||
p += v * __real(b[i]);
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
integer
|
||||
main(void)
|
||||
{
|
||||
o_(dp(l_effect(1r, 3r, -5r), l_effect(4r, -2r, -1r)), "\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,9 +1,11 @@
|
|||
-- DOT PRODUCT ---------------------------------------------------------------
|
||||
|
||||
-- dotProduct :: [Number] -> [Number] -> Number
|
||||
on dotProduct(xs, ys)
|
||||
script product
|
||||
on lambda(a, b)
|
||||
on |λ|(a, b)
|
||||
a * b
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
if length of xs = length of ys then
|
||||
|
|
@ -13,19 +15,8 @@ on dotProduct(xs, ys)
|
|||
end if
|
||||
end dotProduct
|
||||
|
||||
-- sum :: [Number] -> Number
|
||||
on sum(xs)
|
||||
script add
|
||||
on lambda(a, b)
|
||||
a + b
|
||||
end lambda
|
||||
end script
|
||||
|
||||
foldl(add, 0, xs)
|
||||
end sum
|
||||
|
||||
|
||||
-- TEST
|
||||
-- TEST ----------------------------------------------------------------------
|
||||
on run
|
||||
|
||||
dotProduct([1, 3, -5], [4, -2, -1])
|
||||
|
|
@ -34,18 +25,7 @@ on run
|
|||
end run
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS
|
||||
|
||||
-- all :: (a -> Bool) -> [a] -> Bool
|
||||
on all(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
if not lambda(item i of xs) then return false
|
||||
end repeat
|
||||
true
|
||||
end tell
|
||||
end all
|
||||
-- GENERIC FUNCTIONS ---------------------------------------------------------
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
|
|
@ -53,29 +33,20 @@ on foldl(f, startValue, xs)
|
|||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to lambda(v, item i of xs, i, xs)
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
on zipWith(f, xs, ys)
|
||||
set nx to length of xs
|
||||
set ny to length of ys
|
||||
if nx < 1 or ny < 1 then
|
||||
{}
|
||||
-- min :: Ord a => a -> a -> a
|
||||
on min(x, y)
|
||||
if y < x then
|
||||
y
|
||||
else
|
||||
set lng to cond(nx < ny, nx, ny)
|
||||
set lst to {}
|
||||
tell mReturn(f)
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to lambda(item i of xs, item i of ys)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
x
|
||||
end if
|
||||
end zipWith
|
||||
end min
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
|
|
@ -84,16 +55,30 @@ on mReturn(f)
|
|||
f
|
||||
else
|
||||
script
|
||||
property lambda : f
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- cond :: Bool -> (a -> b) -> (a -> b) -> (a -> b)
|
||||
on cond(bool, f, g)
|
||||
if bool then
|
||||
f
|
||||
else
|
||||
g
|
||||
end if
|
||||
end cond
|
||||
-- sum :: [Number] -> Number
|
||||
on sum(xs)
|
||||
script add
|
||||
on |λ|(a, b)
|
||||
a + b
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
foldl(add, 0, xs)
|
||||
end sum
|
||||
|
||||
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
on zipWith(f, xs, ys)
|
||||
set lng to min(length of xs, length of ys)
|
||||
set lst to {}
|
||||
tell mReturn(f)
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, item i of ys)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end zipWith
|
||||
|
|
|
|||
19
Task/Dot-product/Bc/dot-product.bc
Normal file
19
Task/Dot-product/Bc/dot-product.bc
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
/* Calculate the dot product of two vectors a and b (represented as
|
||||
* arrays) of size n.
|
||||
*/
|
||||
define d(a[], b[], n) {
|
||||
auto d, i
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
d += a[i] * b[i]
|
||||
}
|
||||
return(d)
|
||||
}
|
||||
|
||||
a[0] = 1
|
||||
a[1] = 3
|
||||
a[2] = -5
|
||||
b[0] = 4
|
||||
b[1] = -2
|
||||
b[2] = -1
|
||||
d(a[], b[], 3)
|
||||
|
|
@ -8,7 +8,13 @@
|
|||
(map #(apply * %))
|
||||
(reduce +)))
|
||||
|
||||
(defn dot-product3
|
||||
"Dot product of vectors. Tested on version 1.8.0."
|
||||
[v1 v2]
|
||||
{:pre [(= (count v1) (count v2))]}
|
||||
(reduce + (map * v1 v2)))
|
||||
|
||||
;Example Usage
|
||||
(println (dot-product [1 3 -5] [4 -2 -1]))
|
||||
(println (dot-product2 [1 3 -5] [4 -2 -1]))
|
||||
(println (dot-product3 [1 3 -5] [4 -2 -1]))
|
||||
|
|
|
|||
26
Task/Dot-product/Component-Pascal/dot-product.component
Normal file
26
Task/Dot-product/Component-Pascal/dot-product.component
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
MODULE DotProduct;
|
||||
IMPORT StdLog;
|
||||
|
||||
PROCEDURE Calculate*(x,y: ARRAY OF INTEGER): INTEGER;
|
||||
VAR
|
||||
i,sum: INTEGER;
|
||||
BEGIN
|
||||
sum := 0;
|
||||
FOR i:= 0 TO LEN(x) - 1 DO
|
||||
INC(sum,x[i] * y[i]);
|
||||
END;
|
||||
RETURN sum
|
||||
END Calculate;
|
||||
|
||||
PROCEDURE Test*;
|
||||
VAR
|
||||
i,sum: INTEGER;
|
||||
v1,v2: ARRAY 3 OF INTEGER;
|
||||
BEGIN
|
||||
v1[0] := 1;v1[1] := 3;v1[2] := -5;
|
||||
v2[0] := 4;v2[1] := -2;v2[2] := -1;
|
||||
|
||||
StdLog.Int(Calculate(v1,v2));StdLog.Ln
|
||||
END Test;
|
||||
|
||||
END DotProduct.
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
dotp :: Num a => [a] -> [a] -> a
|
||||
dotp a b | length a == length b = sum (zipWith (*) a b)
|
||||
| otherwise = error "Vector sizes must match"
|
||||
|
||||
15
Task/Dot-product/Haskell/dot-product-2.hs
Normal file
15
Task/Dot-product/Haskell/dot-product-2.hs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
dotp
|
||||
:: Num a
|
||||
=> [a] -> [a] -> Maybe a
|
||||
dotp a b
|
||||
| length a == length b = Just $ sum (zipWith (*) a b)
|
||||
| otherwise = Nothing
|
||||
|
||||
main :: IO ()
|
||||
main = mbPrint $ dotp [1, 3, -5] [4, -2, -1] -- prints 3
|
||||
|
||||
mbPrint
|
||||
:: Show a
|
||||
=> Maybe a -> IO ()
|
||||
mbPrint (Just x) = print x
|
||||
mbPrint n = print n
|
||||
6
Task/Dot-product/Hy/dot-product.hy
Normal file
6
Task/Dot-product/Hy/dot-product.hy
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
(defn dotp [a b]
|
||||
(assert (= (len a) (len b)))
|
||||
(sum (genexpr (* aterm bterm)
|
||||
[(, aterm bterm) (zip a b)])))
|
||||
|
||||
(assert (= 3 (dotp [1 3 -5] [4 -2 -1])))
|
||||
|
|
@ -1,4 +1,6 @@
|
|||
fun dot(v1: Array<Double>, v2: Array<Double>) =
|
||||
v1.zip(v2).map { it.first * it.second }.reduce { a, b -> a + b }
|
||||
v1.zip(v2).map { it.first * it.second }.reduce { a, b -> a + b }
|
||||
|
||||
dot(arrayOf(1.0, 3.0, -5.0), arrayOf(4.0, -2.0, -1.0)).let { println(it) }
|
||||
fun main(args: Array<String>) {
|
||||
dot(arrayOf(1.0, 3.0, -5.0), arrayOf(4.0, -2.0, -1.0)).let { println(it) }
|
||||
}
|
||||
|
|
|
|||
4
Task/Dot-product/NewLISP/dot-product.newlisp
Normal file
4
Task/Dot-product/NewLISP/dot-product.newlisp
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(define (dot-product x y)
|
||||
(apply + (map * x y)))
|
||||
|
||||
(println (dot-product '(1 3 -5) '(4 -2 -1)))
|
||||
|
|
@ -1,17 +1,16 @@
|
|||
/*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 /*display a blank line for readability.*/
|
||||
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=dotProd(vectorA, vectorB) /*invoke function & compute dot product*/
|
||||
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. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
dotProd: procedure; parse arg A,B /*this function compute the dot product*/
|
||||
$=0 /*initialize the sum to 0 (zero). */
|
||||
do j=1 for words(A) /*multiply each number in the vectors. */
|
||||
$=$+word(A,j) * word(B,j) /* ··· and add the product to the sum.*/
|
||||
end /*j*/
|
||||
return $ /*return the sum to invoker of function*/
|
||||
.Prod: procedure; parse arg A,B /*this function compute the dot product*/
|
||||
$=0 /*initialize the sum to 0 (zero). */
|
||||
do j=1 for words(A) /*multiply each number in the vectors. */
|
||||
$=$+word(A,j) * word(B,j) /* ··· and add the product to the sum.*/
|
||||
end /*j*/
|
||||
return $ /*return the sum to function's invoker.*/
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
/*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 /*display a blank line for readability.*/
|
||||
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*/
|
||||
|
|
@ -9,8 +8,6 @@ say /*display a blank line for read
|
|||
say 'dot product = ' p /*display the dot product to terminal. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
ser: say "***error*** vector " @.k ' element' j " isn't numeric: " n.k; exit 13
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
.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. */
|
||||
|
|
@ -24,8 +21,10 @@ ser: say "***error*** vector " @.k ' element' j " isn't numeric: " n.k;
|
|||
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 call ser
|
||||
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 invoker of function*/
|
||||
return $ /*return the sum to function's invoker.*/
|
||||
|
|
|
|||
4
Task/Dot-product/Stata/dot-product-1.stata
Normal file
4
Task/Dot-product/Stata/dot-product-1.stata
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
matrix a=1,3,-5
|
||||
matrix b=4,-2,-1
|
||||
matrix c=a*b'
|
||||
di el("c",1,1)
|
||||
4
Task/Dot-product/Stata/dot-product-2.stata
Normal file
4
Task/Dot-product/Stata/dot-product-2.stata
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
matrix a=1\3\-5
|
||||
matrix b=4\-2\-1
|
||||
matrix c=a'*b
|
||||
di el("c",1,1)
|
||||
3
Task/Dot-product/Stata/dot-product-3.stata
Normal file
3
Task/Dot-product/Stata/dot-product-3.stata
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
a=1,3,-5
|
||||
b=4,-2,-1
|
||||
a*b'
|
||||
3
Task/Dot-product/Stata/dot-product-4.stata
Normal file
3
Task/Dot-product/Stata/dot-product-4.stata
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
a=1\3\-5
|
||||
b=4\-2\-1
|
||||
a'*b
|
||||
1
Task/Dot-product/Stata/dot-product-5.stata
Normal file
1
Task/Dot-product/Stata/dot-product-5.stata
Normal file
|
|
@ -0,0 +1 @@
|
|||
sum(a:*b)
|
||||
1
Task/Dot-product/Zkl/dot-product-1.zkl
Normal file
1
Task/Dot-product/Zkl/dot-product-1.zkl
Normal file
|
|
@ -0,0 +1 @@
|
|||
fcn dotp(a,b){Utils.zipWith('*,a,b).sum()}
|
||||
3
Task/Dot-product/Zkl/dot-product-2.zkl
Normal file
3
Task/Dot-product/Zkl/dot-product-2.zkl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fcn dotp2(a,b){if(a.len()!=b.len())throw(Exception.ValueError);
|
||||
Utils.zipWith('*,a,b).sum()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue