RosettaCodeData/Task/Dot-product/Objective-C/dot-product.m

108 lines
2.3 KiB
Mathematica
Raw Permalink Normal View History

2013-04-10 22:43:41 -07:00
#import <stdio.h>
#import <stdint.h>
#import <stdlib.h>
#import <string.h>
2014-04-02 16:56:35 +00:00
#import <Foundation/Foundation.h>
2013-04-10 22:43:41 -07:00
// this class exists to return a result between two
// vectors: if vectors have different "size", valid
// must be NO
2014-04-02 16:56:35 +00:00
@interface VResult : NSObject
2013-04-10 22:43:41 -07:00
{
@private
double value;
BOOL valid;
}
2014-04-02 16:56:35 +00:00
+(instancetype)new: (double)v isValid: (BOOL)y;
-(instancetype)init: (double)v isValid: (BOOL)y;
2013-04-10 22:43:41 -07:00
-(BOOL)isValid;
-(double)value;
@end
@implementation VResult
2014-04-02 16:56:35 +00:00
+(instancetype)new: (double)v isValid: (BOOL)y
2013-04-10 22:43:41 -07:00
{
2014-04-02 16:56:35 +00:00
return [[self alloc] init: v isValid: y];
2013-04-10 22:43:41 -07:00
}
2014-04-02 16:56:35 +00:00
-(instancetype)init: (double)v isValid: (BOOL)y
2013-04-10 22:43:41 -07:00
{
2014-04-02 16:56:35 +00:00
if ((self == [super init])) {
value = v;
valid = y;
}
2013-04-10 22:43:41 -07:00
return self;
}
-(BOOL)isValid { return valid; }
-(double)value { return value; }
@end
2014-04-02 16:56:35 +00:00
@interface RCVector : NSObject
2013-04-10 22:43:41 -07:00
{
@private
double *vec;
uint32_t size;
}
2014-04-02 16:56:35 +00:00
+(instancetype)newWithArray: (double *)v ofLength: (uint32_t)l;
-(instancetype)initWithArray: (double *)v ofLength: (uint32_t)l;
2013-04-10 22:43:41 -07:00
-(VResult *)dotProductWith: (RCVector *)v;
-(uint32_t)size;
-(double *)array;
-(void)free;
@end
@implementation RCVector
2014-04-02 16:56:35 +00:00
+(instancetype)newWithArray: (double *)v ofLength: (uint32_t)l
2013-04-10 22:43:41 -07:00
{
2014-04-02 16:56:35 +00:00
return [[self alloc] initWithArray: v ofLength: l];
2013-04-10 22:43:41 -07:00
}
2014-04-02 16:56:35 +00:00
-(instancetype)initWithArray: (double *)v ofLength: (uint32_t)l
2013-04-10 22:43:41 -07:00
{
2014-04-02 16:56:35 +00:00
if ((self = [super init])) {
size = l;
vec = malloc(sizeof(double) * l);
if ( vec == NULL )
return nil;
2013-04-10 22:43:41 -07:00
memcpy(vec, v, sizeof(double)*l);
}
2014-04-02 16:56:35 +00:00
return self;
2013-04-10 22:43:41 -07:00
}
2014-04-02 16:56:35 +00:00
-(void)dealloc
2013-04-10 22:43:41 -07:00
{
free(vec);
}
-(uint32_t)size { return size; }
-(double *)array { return vec; }
-(VResult *)dotProductWith: (RCVector *)v
{
double r = 0.0;
uint32_t i, s;
double *v1;
if ( [self size] != [v size] ) return [VResult new: r isValid: NO];
s = [self size];
v1 = [v array];
for(i = 0; i < s; i++) {
r += vec[i] * v1[i];
}
return [VResult new: r isValid: YES];
}
@end
double val1[] = { 1, 3, -5 };
double val2[] = { 4,-2, -1 };
int main()
{
2014-04-02 16:56:35 +00:00
@autoreleasepool {
RCVector *v1 = [RCVector newWithArray: val1 ofLength: sizeof(val1)/sizeof(double)];
RCVector *v2 = [RCVector newWithArray: val2 ofLength: sizeof(val1)/sizeof(double)];
VResult *r = [v1 dotProductWith: v2];
if ( [r isValid] ) {
printf("%lf\n", [r value]);
} else {
fprintf(stderr, "length of vectors differ\n");
}
2013-04-10 22:43:41 -07:00
}
return 0;
}