RosettaCodeData/Task/Dot-product/FutureBasic/dot-product.basic

42 lines
1.4 KiB
Plaintext

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