博客
关于我
Objective-C实现Polynomials多项式算法 (附完整源码)
阅读量:793 次
发布时间:2023-02-19

本文共 1241 字,大约阅读时间需要 4 分钟。

Polynomial类Objective-C实现多项式算法示例

Polynomial.h文件:

#import 
@interface Polynomial : NSObject@property (nonatomic, strong) NSMutableArray *coefficients;@end

Polynomial.m文件:

@implementation Polynomial- (id)init {    self = [NSObject new];    self.coefficients = [NSMutableArray new];    return self;}- (void)addTerm:(double)term {    [self.coefficients addObject:term];}- (void)removeTerm:(double)term {    [self.coefficients removeObject:term];}- (void)multiplyByPolynomial:(Polynomial *)anotherPolynomial {    NSMutableArray *newCoefficients = [NSMutableArray new];    for (double coefficient : self.coefficients) {        for (double anotherCoefficient : anotherPolynomial.coefficients) {            [newCoefficients addObject:coefficient * anotherCoefficient];        }    }    self.coefficients = newCoefficients;}- (double)evaluateAtX:(double)x {    double result = 0.0;    for (double coefficient : self.coefficients) {        result += coefficient * pow(x, [self.degree]);        [self.coefficients removeObject:0.0]; // 移除零项    }    return result;}- (int)degree {    return [self.coefficients count] - 1;}

Polynomial类实现了多项式的基本操作,包括:

  • 项的添加和移除
  • 与另一个多项式的乘法
  • 在给定x值处的值评估
  • 获取多项式的次数
  • 通过这种方式,我们可以创建一个多项式对象,并通过上述方法进行操作。这个实现简化了多项式运算,使其易于使用和扩展。

    转载地址:http://benfk.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现NLP中文分词(附完整源码)
    查看>>
    Objective-C实现NMS非极大值抑制(附完整源码)
    查看>>
    Objective-C实现NMS非极大值抑制(附完整源码)
    查看>>
    Objective-C实现Node.Js中生成一个UUID/GUID算法(附完整源码)
    查看>>
    Objective-C实现not gate非门算法(附完整源码)
    查看>>
    Objective-C实现NQueen皇后问题算法(附完整源码)
    查看>>
    Objective-C实现number of digits解字符数算法(附完整源码)
    查看>>
    Objective-C实现NumberOfIslands岛屿的个数算法(附完整源码)
    查看>>
    Objective-C实现numerical integration数值积分算法(附完整源码)
    查看>>
    Objective-C实现n个取m个数的组合算法(附完整源码)
    查看>>
    Objective-C实现N数理论(质素相关)算法(附完整源码)
    查看>>
    Objective-C实现n皇后问题算法(附完整源码)
    查看>>
    Objective-C实现O(E + V) 中找到 0-1-graph 中的最短路径算法(附完整源码)
    查看>>
    Objective-C实现OCR文字识别(附完整源码)
    查看>>
    Objective-C实现odd even sort奇偶排序算法(附完整源码)
    查看>>
    Objective-C实现ohms law欧姆定律算法(附完整源码)
    查看>>
    Objective-C实现P-Series algorithm算法(附完整源码)
    查看>>
    Objective-C实现page rank算法(附完整源码)
    查看>>
    Objective-C实现PageRank算法(附完整源码)
    查看>>
    Objective-C实现pancake sort煎饼排序算法(附完整源码)
    查看>>