2012-10-19 1 views
10

"Complex Numbers"はObjective-Cですでに定義されていますか?

- Objective-Cは、複素数に "i"を追加する方法を知っていますか? Complex.mファイルに "real"と "imaginary"をdouble値として定義したとき、私はXcodeが "real"と "imaginary"がdouble値であることを知っているだけだと考えました。

- たとえば、 "myComplex.imaginary = 7;"にすると、main.mファイルの複素数の末尾に "i"を追加すると、 "myComplex.imaginary = 7i;"にするその行の出力は0.00000iになります。他の文字を追加すると、プログラムは単に実行されません。どうしてですか?

"本物の"と "想像的"の意味は、Xcodeにはすでに知られていますが、私が従う本はこれを指定していないので少し混乱しています。

また、次のコードを作成していないことに注意してください。自分の問題を把握できなかったため、このコードは書籍フォーラムのメンバーからコピーされました。

// Complex.h 

#include <Foundation/Foundation.h> 

@interface Complex : NSObject 
@property double real, imaginary; 
-(void) print; 
-(Complex *) add: (Complex *) complexNum; 
-(Complex *) subtract: (Complex *) complexNum; 
-(Complex *) multiply: (Complex *) complexNum; 
-(Complex *) divide: (Complex *) complexNum; 
@end 

// Complex.m 

#import "Complex.h" 

@implementation Complex 
@synthesize real, imaginary; 

-(void) print 
{ 
    NSLog(@"%f + %fi", real, imaginary); 
} 
-(Complex *) add: (Complex *) complexNum 
{ 
    Complex *result = [[Complex alloc]init]; 
    result.real = real + complexNum.real; 
    result.imaginary = imaginary + complexNum.imaginary; 
    return result; 
} 
-(Complex *) subtract: (Complex *) complexNum 
{ 
    Complex *result = [[Complex alloc]init]; 
    result.real = real - complexNum.real; 
    result.imaginary = imaginary - complexNum.imaginary; 
    return result; 
} 
-(Complex *) multiply: (Complex *) complexNum 
{ 
    Complex *result = [[Complex alloc]init]; 
    result.real = real * complexNum.real; 
    result.imaginary = imaginary * complexNum.imaginary; 
    return result; 
} 
-(Complex *) divide: (Complex *) complexNum 
{ 
    Complex *result = [[Complex alloc]init]; 
    result.real = real/complexNum.real; 
    result.imaginary = imaginary/complexNum.imaginary; 
    return result; 
} 
@end 

// 
// main.m 
// Complex 

#include <Foundation/Foundation.h> 
#import "Complex.h" 

int main(int argc, const char *argv[]) { 

    @autoreleasepool { 
     Complex *myComplex = [[Complex alloc]init]; 
     Complex *totalComplex = [[Complex alloc]init]; 
     Complex *yourComplex = [[Complex alloc]init]; 

     myComplex.real = 5.3; 
     myComplex.imaginary = 7; 
     [myComplex print]; 
     NSLog(@"+"); 

     yourComplex.real = 2.7; 
     yourComplex.imaginary = 4; 
     [yourComplex print]; 
     NSLog(@"="); 

     totalComplex = [myComplex add: yourComplex]; 
     [totalComplex print]; 
    } 
    return 0; 
} 
+5

ああ!理解できない英語でのn00b以外の質問!何がSOに起こったのですか? (+1) –

答えて

12

複素数型は、対物-Cの現代版でのスーパーセットである、C99で定義されています。 The actual syntax is like

#include <complex.h> 

... 

complex double z = 2.7 + 3.4*I; 
complex double w = 4.5 - 1.7*I; 
complex double t = z*w; 
printf("%g + %gi", creal(t), cimag(t)); 

iサフィックスがextension coming from GCCされていること。 Xcodeで使用されるコンパイラ(clang)には、GCCと互換性のあるほとんどの機能がありますので、3.4iと記述してもエラーはありません。


そして、あなたの質問のために、

  • 方法のObjective-Cは複素数に "I" を追加するために知っているのですか?

出力を意味する場合、Objective-Cは "i"を追加することを認識しません。 "myComplex.imaginary = 7;" それは "私" だけあなたが

-(void) print 
{ 
    NSLog(@"%f + %fi", real, imaginary); 
//    ^
} 
  • 私はオンにした場合にそれを告げたので、印刷します"myComplex.imaginary = 7i;"にする7Iは虚数である、とmyComplex.imaginaryは、このように本当番号、「ダブル」であるため、その行の出力が0.00000i

になります。 Cの標準では、実数値と虚数値の変換時にはゼロを得ることを推奨しています(C99§G.4.2/ 1)。したがって、あなたが書いたものはmyComplex.imaginary = 0.0;です。私は、他の文字を追加した場合

  • 、プログラムは単純に実行されません、これはなぜですか?

実際にあなたが7.0ifのようなものを書くことができます。ここでも、これはObjective-Cが適応したCのものです。fを追加して、デフォルトのタイプ "double"から "float"に10進数を変換することができます。また、GCCは、iを追加して実数を虚数に変える追加機能を追加します。 7.0xのようなその他のものは、xの意味を知らないためコンパイラが停止する原因になります。

+0

ハハ、それは、明らかに私はあなたに追いつくことができなかった;) –

+0

ありがとう!私は完全に理解していて、NSLogの "%fi"を次の章に気付かなかった。 – Ronald

7

C99には複素数のネイティブサポートが追加されているので、通常の浮動小数点数や整数のように扱いやすくなりました。もはや醜い構造体はありません!数値の浮動小数点表現でトリックを行うと、_Complex_Iとそれに相当するIマクロの値は、実数で掛け合わせると、double complexまたはfloat complexの数になります(complexは新しいタイプ修飾語です) C99でも紹介されている)。この新しい便利な機能で、あなたは

#include <complex.h> 

double complex z1 = 2.0 + 3.0 * I; 
double complex z2 = 1.5 - 2.0 * I; 
double complex prod = z1 * z2; 

printf("Product = %f + %f\n", creal(prod), cimag(prod)); 

ように簡単にCで複素数計算を行うことができますので、同様にこの程度the GNU explanationを確認してください。

i接尾辞はC99言語のGNU拡張です。したがって、それは非標準です。それにもかかわらず、Xcode(GCCとClang)が使用する両方のコンパイラがこの拡張を実装しています。

(追記:。。。XcodeのコンパイラとIDEを混同しないでください。この程度を知らないのXcode自体はコンパイルを実行しない - その背後にあるコンパイラは行う)

+0

ありがとう、GNUの説明の方程式は役に立ちました。 – Ronald

1

2つの重大な間違いがあります。クラス複合体の実装 - 複素数は絶対に間違った方法で乗算され、分割されます! 2つの複素数の実数部と虚数部を単純に乗算または除算するだけでは不十分です。その場合、乗法と除算の式を使用する必要があります.Googleにはそれに関するエントリがたくさんあると思います。今は誤ったコードなので、書き直す必要があります。

乗算にとっては、この

-(Complex *)mul:(Complex *)n 
{ 
    Complex *res = [[Complex alloc]init]; 
    res.re = re * n.re - im * n.im; 
    res.im = re * n.im + im * n.re; 
    return res; 
} 
2

のようなものでなければなりませんここで私は私のプロジェクトのために開発した複素数を操作するためのクラスです。それは誰かにとって有益なことかもしれません。これには、標準的な加算、減算、乗算、および除算の方法が含まれています。さらに、複素数の法と係数を計算する方法もあります。そして、最後に、それは時に高速フーリエ変換との契約

#import <Foundation/Foundation.h> 

@interface Complex : NSObject 
@property double re, im; 
-(Complex *)add :(Complex *) n; 
-(Complex *)sub :(Complex *) n; 
-(Complex *)mul :(Complex *) n; 
-(Complex *)div :(Complex *) n; 
+(Complex *)wkn :(int) k :(int) n; 
-(double)mod; 
-(double)arg; 
@end 

#import "Complex.h" 

@implementation Complex 
@synthesize re, im; 
// Addition of two complex numbers 
-(Complex *)add:(Complex *)n 
{ 
    Complex *res = [[Complex alloc]init]; 
    res.re = re + n.re; 
    res.im = im + n.im; 
    return res; 
} 
// Subtraction of two complex numbers 
-(Complex *)sub:(Complex *)n 
{ 
    Complex *res = [[Complex alloc]init]; 
    res.re = re - n.re; 
    res.im = im - n.im; 
    return res; 
} 
// Multiplication of two complex numbers 
-(Complex *)mul:(Complex *)n 
{ 
    Complex *res = [[Complex alloc]init]; 
    res.re = re * n.re - im * n.im; 
    res.im = re * n.im + im * n.re; 
    return res; 
} 
// Division of two complex numbers 
-(Complex *)div: (Complex *)n 
{ 
    Complex *res = [[Complex alloc]init]; 
    double A = (pow(n.re, 2.0) + pow(n.im, 2.0)); 
    res.re = (re * n.re - im * n.im)/A; 
    res.im = (im * n.re - re * n.im)/A; 
    return res; 
} 
// Modulus of complex number 
-(double)mod 
{ 
    double res = sqrt(pow(re, 2.0) + pow(im, 2.0)); 
    return res; 
} 
// Argument of complex number 
-(double)arg 
{ 
    double res; int quad; 
    if (re == 0 && im > 0) res = M_PI_2; 
    else if (re == 0 && im < 0) res = 3 * M_PI_2; 
    else 
    { 
     if (re > 0 && im >= 0) quad = 1; 
     else if (re < 0 && im >= 0) quad = 2; 
     else if (re < 0 && im < 0) quad = 3; 
     else if (re > 0 && im < 0) quad = 4; 
     double temp = atan(im/re); 
     switch (quad) 
     { 
      case 1: 
       res = temp; 
       break; 
      case 4: 
       res = 2 * M_PI + temp; 
       break; 
      case 2: case 3: 
       res = M_PI + temp; 
       break; 
     } 
    } 
    return res; 
} 
// Turning factor calculation for "butterfly" FFT algorithm 
+(Complex *)wkn:(int)k :(int)n 
{ 
    Complex *res = [[Complex alloc]init]; 
    res.re = cos(2 * M_PI * k/n); 
    res.im = -sin(2 * M_PI * k/n); 
    return res; 
} 

@end 

「蝶」アルゴリズムのために有用である回し因子(複雑な指数)を算出するためのクラスメソッドを持っているあなたの忍耐をありがとう)

関連する問題