2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,8 +1,20 @@
Create a function/use an in-built function, to compute the '''[[wp:Dot product|dot product]]''', also known as the '''scalar product''' of two vectors. If possible, make the vectors of arbitrary length.
;Task:
Create a function/use an in-built function, to compute the   '''[[wp:Dot product|dot product]]''',   also known as the   '''scalar product'''   of two vectors.
As an example, compute the dot product of the vectors <code>[1, 3, -5]</code> and <code>[4, -2, -1]</code>.
If possible, make the vectors of arbitrary length.
If implementing the dot product of two vectors directly, each vector must be the same length; multiply corresponding terms from each vector then sum the results to produce the answer.
;Reference:
* [[Vector products]] here on RC.
As an example, compute the dot product of the vectors:
:::: &nbsp; <big> <code> [1, &nbsp;3, -5] </code> </big> &nbsp; &nbsp; and
:::: &nbsp; <big> <code> [4, -2, -1] </code> </big>
<br>
If implementing the dot product of two vectors directly:
:::* &nbsp; each vector must be the same length
:::* &nbsp; multiply corresponding terms from each vector
:::* &nbsp; sum the products &nbsp; (to produce the answer)
;Related task:
* &nbsp; [[Vector products]]
<br><br>

View file

@ -0,0 +1,24 @@
* Dot product 03/05/2016
DOTPROD CSECT
USING DOTPROD,R15
SR R7,R7 p=0
LA R6,1 i=1
LOOPI CH R6,=AL2((B-A)/4) do i=1 to hbound(a)
BH ELOOPI
LR R1,R6 i
SLA R1,2 *4
L R3,A-4(R1) a(i)
L R4,B-4(R1) b(i)
MR R2,R4 a(i)*b(i)
AR R7,R3 p=p+a(i)*b(i)
LA R6,1(R6) i=i+1
B LOOPI
ELOOPI XDECO R7,PG edit p
XPRNT PG,80 print buffer
XR R15,R15 rc=0
BR R14 return
A DC F'1',F'3',F'-5'
B DC F'4',F'-2',F'-1'
PG DC CL80' ' buffer
YREGS
END DOTPROD

View file

@ -0,0 +1,99 @@
-- dotProduct :: [Number] -> [Number] -> Number
on dotProduct(xs, ys)
script product
on lambda(a, b)
a * b
end lambda
end script
if length of xs = length of ys then
sum(zipWith(product, xs, ys))
else
missing value -- arrays of differing dimension
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
on run
dotProduct([1, 3, -5], [4, -2, -1])
--> 3
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
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
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)
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
{}
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
end if
end zipWith
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property lambda : 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

View file

@ -0,0 +1 @@
3

View file

@ -0,0 +1,14 @@
#include <valarray>
#include <iostream>
int main()
{
std::valarray<double> xs = {1,3,-5};
std::valarray<double> ys = {4,-2,-1};
double result = (xs * ys).sum();
std::cout << result << '\n';
return 0;
}

View file

@ -0,0 +1,21 @@
(() => {
'use strict';
// dotProduct :: [Int] -> [Int] -> Int
const dotProduct = (xs, ys) => {
const sum = xs => xs ? xs.reduce((a, b) => a + b, 0) : undefined;
return xs.length === ys.length ? (
sum(zipWith((a, b) => a * b, xs, ys))
) : undefined;
}
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
const zipWith = (f, xs, ys) => {
const ny = ys.length;
return (xs.length <= ny ? xs : xs.slice(0, ny))
.map((x, i) => f(x, ys[i]));
}
return dotProduct([1, 3, -5], [4, -2, -1]);
})();

View file

@ -0,0 +1 @@
3

View file

@ -0,0 +1,4 @@
fun dot(v1: Array<Double>, v2: Array<Double>) =
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) }

View file

@ -0,0 +1,25 @@
MODULE DotProduct;
IMPORT
Out := NPCT:Console;
VAR
x,y: ARRAY 3 OF LONGINT;
PROCEDURE DotProduct(a,b: ARRAY OF LONGINT): LONGINT;
VAR
resp, i: LONGINT;
BEGIN
ASSERT(LEN(a) = LEN(b));
resp := 0;
FOR i := 0 TO LEN(x) - 1 DO
INC(resp,x[i]*y[i])
END;
RETURN resp
END DotProduct;
BEGIN
x[0] := 1;y[0] := 4;
x[1] := 3;y[1] := -2;
x[2] := -5;y[2] := -1;
Out.Int(DotProduct(x,y),0);Out.Ln
END DotProduct.

View file

@ -1,7 +1,5 @@
function dotproduct( $a, $b) {
$i = $res = 0
$a | foreach{ $($_)*$b[$i++] } | foreach{ $res += $_ }
$res
$a | foreach -Begin {$i = $res = 0} -Process { $res += $_*$b[$i++] } -End{$res}
}
dotproduct (1..2) (1..2)
dotproduct (1..10) (11..20)

View file

@ -1,15 +1,17 @@
/*REXX program computes the dot product of two equal size vectors. */
vectorA = ' 1 3 -5 ' /*populate vector A with some numbers*/
vectorB = ' 4 -2 -1 ' /* " " B " " " */
say; 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*/
say; say 'dot product = ' p /*blank line; display the dot product.*/
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*/
/*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.*/
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*/
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*/

View file

@ -1,34 +1,31 @@
/*REXX program computes the dot product of two equal size vectors. */
vectorA = ' 1 3 -5 ' /*populate vector A with some numbers*/
vectorB = ' 4 -2 -1 ' /* " " B " " " */
say; 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*/
say; say 'dot product = ' p /*blank line; display the dot product.*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
dotProd: procedure; parse arg A,B /*this function compute the dot product*/
lenA = words(A) /*the number of numbers in vector A. */
lenB = words(B) /* " " " " " " B. */
e='***error!*** '; @.='A'; @.2='B' /*define some literals for error msgs. */
if lenA\==lenB then do /*oops─ay.*/
say e "vectors aren't the same size:"
say ' vector A length = ' lenA
say ' vector B length = ' lenB
exit 13 /*exit 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. */
n.1=word(A,j); n.2=word(B,j)
do k=1 for 2; notNum=\datatype(n.k,'Number') /*verify numbers.*/
if notNum then do /*oops─ay, ¬ num.*/
say e "vector" @.k 'element' j "isn't numeric:" n.k
exit 13 /*exit with return code 13.*/
end
end /*k*/
$=$+n.1 * n.2 /* ··· and add the product to the sum.*/
end /*j*/
return $ /*return the sum to invoker of function*/
/*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.*/
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. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
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. */
/*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 call ser
end /*k*/
$=$ + #.1 * #.2 /* ··· and add the product to the sum.*/
end /*j*/
return $ /*return the sum to invoker of function*/

View file

@ -0,0 +1 @@
print(sum([1, 3, -5] * [4, -2, -1]));

View file

@ -0,0 +1 @@
sum({1,3,5}*{4,2,1})

View file

@ -0,0 +1,70 @@
format PE64 console
entry start
include 'win64a.inc'
section '.text' code readable executable
start:
stdcall dotProduct, vA, vB
invoke printf, msg_num, rax
stdcall dotProduct, vA, vC
invoke printf, msg_num, rax
invoke ExitProcess, 0
proc dotProduct vectorA, vectorB
mov rax, [rcx]
cmp rax, [rdx]
je .calculate
invoke printf, msg_sizeMismatch
mov rax, 0
ret
.calculate:
mov r8, rcx
add r8, 8
mov r9, rdx
add r9, 8
mov rcx, rax
mov rax, 0
mov rdx, 0
.next:
mov rbx, [r9]
imul rbx, [r8]
add rax, rbx
add r8, 8
add r9, 8
loop .next
ret
endp
section '.data' data readable
msg_num db "%d", 0x0D, 0x0A, 0
msg_sizeMismatch db "Size mismatch; can't calculate.", 0x0D, 0x0A, 0
struc Vector [symbols] {
common
.length dq (.end - .symbols) / 8
.symbols dq symbols
.end:
}
vA Vector 1, 3, -5
vB Vector 4, -2, -1
vC Vector 7, 2, 9, 0
section '.idata' import data readable writeable
library kernel32, 'KERNEL32.DLL',\
msvcrt, 'MSVCRT.DLL'
include 'api/kernel32.inc'
import msvcrt,\
printf, 'printf'

View file

@ -0,0 +1,3 @@
3
Size mismatch; can't calculate.
0

View file

@ -0,0 +1,5 @@
10 DIM a(3): LET a(1)=1: LET a(2)=3: LET a(3)=-5
20 DIM b(3): LET b(1)=4: LET b(2)=-2: LET b(3)=-1
30 LET sum=0
40 FOR i=1 TO 3: LET sum=sum+a(i)*b(i): NEXT i
50 PRINT sum