Jump to content

Dot product: Difference between revisions

no edit summary
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
No edit summary
Line 1,302:
z dot z = 1
y dot z = 0</pre>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn DotProduct( a as CFArrayRef, b as CFArrayRef ) as double
NSInteger i
double dp = 0.0
if len(a) != len(b) then alert 1, NSAlertStyleWarning,, @"Vectors have unequal length.", @"Okay" : exit fn
for i = 0 to len(a)-1
dp += fn NumberDoubleValue( a[i] ) * fn NumberDoubleValue( b[i] )
next
end fn = dp
 
CFArrayRef a, b
NSUInteger i
 
a = @[@1, @3, @-5]
b = @[@4, @-2, @-1]
printf @"Dot product of [%@, %@, %@].[%@, %@, %@] = %.4f", a[0], a[1], a[2], b[0], b[1], b[2], fn DotProduct( a, b )
 
a = @[@1.0, @1.0, @3.14159]
b = @[@-1.0, @2.618033989, @3.0]
printf @"Dot product of [%@, %@, %@].[%@, %@, %@] = %.4f", a[0], a[1], a[2], b[0], b[1], b[2], fn DotProduct( a, b )
 
a = @[@8, @13, @-5]
b = @[@4, @-7, @-11]
printf @"Dot product of [%@, %@, %@].[%@, %@, %@] = %.4f", a[0], a[1], a[2], b[0], b[1], b[2], fn DotProduct( a, b )
 
a = @[@1, @2, @3]
b = @[@7, @8, @9]
printf @"Dot product of [%@, %@, %@].[%@, %@, %@] = %.4f", a[0], a[1], a[2], b[0], b[1], b[2], fn DotProduct( a, b )
 
CFMutableArrayRef x, y
x = fn MutableArrayWithCapacity(0)
y = fn MutableArrayWithCapacity(0)
for i = 0 to 9 : MutableArrayInsertObjectAtIndex( x, fn NumberWithInteger(i+ 1), i ) : next
for i = 0 to 9 : MutableArrayInsertObjectAtIndex( y, fn NumberWithInteger(i+11), i ) : next
printf @"Dot product of [1…10].[11…20] = %.4f", fn DotProduct( fn ArrayWithArray( x ), fn ArrayWithArray( y ) )
NSLog( @"%@", fn WindowPrintViewString( 1 ) )
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Dot product of [1, 3, -5].[4, -2, -1] = 3.0000
Dot product of [1, 1, 3.14159].[-1, 2.618033989, 3] = 11.0428
Dot product of [8, 13, -5].[4, -7, -11] = -4.0000
Dot product of [1, 2, 3].[7, 8, 9] = 50.0000
Dot product of [1…10].[11…20] = 935.0000
</pre>
 
 
 
 
 
=={{header|Fōrmulæ}}==
723

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.