forked from natikgadzhi/XNMaths
-
Notifications
You must be signed in to change notification settings - Fork 1
/
XNFunction+LagrangeInterpolation.m
51 lines (38 loc) · 1.24 KB
/
XNFunction+LagrangeInterpolation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// XNFunction+LagrangeInterpolation.m
//
// Provides XNFuntion as a lagrange polynome initialization category implementation.
// Creates an expression.
//
// Accuracy is a constant.
//
// Created by Нат Гаджибалаев on 12.11.09.
// Copyright 2009 Нат Гаджибалаев. All rights reserved.
//
#import "XNFunction+LagrangeInterpolation.h"
@implementation XNFunction (LagrangeInterpolation)
//
// initializes funtion as an approximation.
- (XNFunction*) initLagrangeInterpolationWithPoints: (NSMutableArray*)aPoints
{
// init our expression template.
NSMutableString* expressionString = [[NSMutableString alloc] initWithCapacity: 50];
for( NSValue* iValue in aPoints ){
NSPoint i = [iValue pointValue];
[expressionString appendString: [NSString stringWithFormat: @"%f*(", i.y ]];
for( NSValue* jValue in aPoints) {
NSPoint j = [jValue pointValue];
if( i.x == j.x )
{
continue;
}
[expressionString appendString: [NSString stringWithFormat: @"((x - %f)/(%f - %f))*",
j.x, i.x, j.x ]];
}
[expressionString appendString: @"1 ) + "];
}
[expressionString appendString: @"0"];
self = [[XNFunction alloc] initWithExpression: expressionString];
return self;
}
@end