Dot product: Difference between revisions
Content added Content deleted
Walterpachl (talk | contribs) (→{{header|REXX}}: fefurbished and dropped np error checking) |
|||
Line 2,816: | Line 2,816: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
With error checking |
|||
<syntaxhighlight lang="rexx">/*REXX program |
<syntaxhighlight lang="rexx">/*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 'vector A =' vectorA /*display the elements of vector A. */ |
|||
Say 'vector B =' vectorB /* " " " " " B. */ |
|||
p= |
p=dot_product(vectorA,vectorB) /*invoke function & compute dot product*/ |
||
Say /*display a blank line for readability.*/ |
|||
Say 'dot product =' p /*display the dot product */ |
|||
Exit /*stick a fork in it, we're all done. */ |
|||
/*------------------------------------------------------------------------------*/ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
dot_product: /* compute the dot product */ |
|||
Parse Arg A,B |
|||
⚫ | |||
/* Begin Error Checking */ |
|||
If words(A)<>words(B) Then |
|||
$=$+word(A,j) * word(B,j) /* ··· and add the product to the sum.*/ |
|||
Call exit 'Vectors aren''t the same size:' words(A) '<>' words(B) |
|||
end /*j*/ |
|||
Do i=1 To words(A) |
|||
return $ /*return the sum to function's invoker.*/</syntaxhighlight> |
|||
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 |
|||
⚫ | |||
⚫ | |||
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: |
'''output''' using the default (internal) inputs: |
||
<pre> |
<pre> |
||
Line 2,838: | Line 2,853: | ||
vector B = 4 -2 -1 |
vector B = 4 -2 -1 |
||
dot product = 3 |
dot product = 3</pre> |
||
</pre> |
|||
===with error checking=== |
|||
<syntaxhighlight lang="rexx">/*REXX program computes the dot product of two equal size vectors (of any size).*/ |
|||
vectorA = ' 1 3 -5 ' /*populate vector A with some numbers*/ |
|||
⚫ | |||
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. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
.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 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 function's invoker.*/</syntaxhighlight> |
|||
'''output''' is the same as the 1<sup>st</sup> REXX version. <br><br> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |