Dot product: Difference between revisions

2,690 bytes removed ,  1 year ago
→‎{{header|REXX}}: fefurbished and dropped np error checking
(→‎{{header|REXX}}: fefurbished and dropped np error checking)
Line 2,816:
 
=={{header|REXX}}==
===noWith error checking===
<syntaxhighlight lang="rexx">/*REXX program computes the dot product of two equal size vectors (of any size).*/
vectorA = ' 1 3 -5 ' vectorA = ' 1 3 -5 ' /*populate vector A with some numbers*/
vectorB = ' 4 -2 -1 ' vectorB = ' 4 -2 -1 ' /* " " B " " " */
saySay 'vector A = ' vectorA /*display the elements in theof vector A. */
saySay 'vector B = ' vectorB /* " " " " " "B. B.*/
p=.Proddot_product(vectorA, vectorB) /*invoke function & compute dot product*/
say Say /*display a blank line for readability.*/
say Say 'dot product = ' p /*display the dot product to terminal. */
exit Exit /*stick a fork in it, we're all done. */
/*------------------------------------------------------------------------------*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
.Proddot_product: procedure; parse arg A,B /*this function compute the dot product */
Parse Arg A,B
$=0 /*initialize the sum to 0 (zero). */
/* Begin Error Checking do j=1 for words(A) /*multiply each number in the vectors. */
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
/* End Error Checking vectorB = ' 4 -2 -1 ' /* " " B " " " */
$product=0 /* initialize the sum to 0 (zero). */
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''' &nbsp; using the default (internal) inputs:
<pre>
Line 2,838 ⟶ 2,853:
vector B = 4 -2 -1
 
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*/
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*/
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''' &nbsp; is the same as the 1<sup>st</sup> REXX version. <br><br>
 
=={{header|Ring}}==
2,300

edits