虚数の計算方法とクラスのオーバーロード

このQ&Aのポイント
  • 虚数の計算方法とは、i^2 = -1、i^3 = -i...などの性質をもつ数の計算方法です。
  • 学校の課題で虚数の計算をする必要がありますが、どのように計算すればよいかわかりません。
  • また、問題で要求されているクラスのオーバーロードの方法についても解説してください。
回答を見る
  • ベストアンサー

虚数の計算

学校の課題で虚数(Complex Number)の計算をしなければいけません。 虚数はi^2=-1, i^3=-i... ですよね? それをどういう風に表現したらいいのかまったくわからないのですが、その部分のヒントいただけないでしょうか? 一応、下のが問題です、、、、。 Write a class called complex to enable operations on complex numbers through the overloaded >> and << operators (to read in and output complex numbers). Complex numbers are of the form: realPart + imaginaryPart*i, where i =sqrt(-1) So overload <<, >>, +, -, *, ==, != to perform the appropriate operations. i.e. Complex1 + Complex2 should add the real parts of both numbers and the two complex parts of both numbers.

  • lilno
  • お礼率22% (6/27)

質問者が選んだベストアンサー

  • ベストアンサー
  • haporun
  • ベストアンサー率40% (230/562)
回答No.2

Cですか? C++ですか? C++ならクラスを定義して、+や/に虚数演算を定義できますが、Cでは虚数と虚数、虚数と実数の演算をするそれぞれの関数を作らなければなりません。 虚数という型はありません。 虚数はR^2のベクトルですから、 typedef struct _COMP{ double x, y; } COMP; などとして、ベクトルのように扱いましょう。 あとはそのデータ型に対する関数を定義するのです。 /*z1 := z1 + z2 = (x1 + x2, y1 + y2)*/ void compadd(COMP *z1, COMP *z2){ z1->x += z2->x; z1->y += z2->y; } /*z1 := z1 * z2 = (x1 * x2 - y1 * y2, x1 * y2 + x2 * y1)*/ void compsqr(COMP *z1, COMP *z2){ double s = z1->x, t = z1->y; z1->x = s * z2->x - t * z2->y; z1->y = s * z2->y + t * z2->x; } /*あと引き算とか割り算とか*/ 虚数はi^2 = -1という定義のしかたもありますが、ベクトル演算として z1 + z2 = (x1 + x2, y1 + y2) z1 * z2 = (x1 * x2 - y1 * y2, x1 * y2 + x2 * y1) などというものを定義する方法もあるのです。

lilno
質問者

お礼

返事が遅くなってすいません。haporunさんのHintで何とかできそうです。ありがとうございます。はじめ課題が出たとき「なんだめちゃかんたんじゃん!」とか思ってしまったのですが、いざやり始めたら結構難しかったです、、。

その他の回答 (1)

  • yurarinco
  • ベストアンサー率34% (20/58)
回答No.1

虚数の計算ではなくて複素数の計算ではないでしょうか? real part と imaginary part をメンバ変数として持つ complex というクラスを作り、<<, >>, +, -, *, ==, != といった演算子をオーバーロードして、いろいろな計算が できるようなclassを作りましょう、という課題では ないでしょうか。 たとえば i は実数部が 0 で虚数部が 1 で、これを 仮に {0, 1} と表したとき、{0, 1}ともう一つの{0, 1} 掛け算したとき、実数部が -1 で虚数部が 0 の{-1, 0} という結果になるような計算を実装してみましょう、 ということだと思います。

関連するQ&A

  • ある英文の一部なのですが和訳できず困っています

    ある英文の一部なのですが和訳できず困っています e is Euler's number, the base of the natural logarithm, i is the imaginary unit, one of the two complex numbers whose square is negative one (the other is -i ), and π is pi, the ratio of the circumference of a circle to its diameter. 最初のあたりの和訳は「eはオイラーの数で自然対数の基礎で、iは虚数単位である。」であると思っているのですがこれも間違っているのでしょうか?

  • as appropriate???

    ある文書に以下のような記述がありました。 Adequacy of plans to include both genders, members of minority groups, children, and their subgroups, as appropriate for the scientific goals of the research. Plans for the recruitment and retention of subjects also will be evaluated. この文においてas appropriate for the scientific goals of the research.とはどういう訳し方をすればいいのでしょうか? また、このasはどういう用法なのでしょうか? ご教授願います。

  • 意味を教えてください

    My first visit to Tokyo was a sensory overload unlike any I’d ever experienced. The sheer bombardment of sights, sounds and smells coupled with the labyrinthine train system are enough to give even an expert gamer synesthesia. I honestly never thought I was going to be able to get around a monstro-megalopolis like this. It took me a few months (and a ton of mistakes) to get some semblance of my bearings, sensory overloadとsemblance of my bearingsの意味を教えてください。よろしくお願いします

  • 次の英文の意味を教えてください。

    It's possible to identify operating constraints for both nominal operation and disturbed situation. 宜しくお願いします。

  • 【至急】和訳をお願いします!m(_ _)m

    【至急】和訳をお願いします!m(_ _)m But the use of numbers alone to represent a food chain can be misleading. A large tree and a single small plant both count as one plant, even though there is an enormous difference in their sizes. For this reason ,it is more appropriate to plot a pyramid of biomass―the mass of living matter at each level of a food chain. Sometimes the pyramid of biomass may be inverted : there is a greater mass of primary consumers than producers.

  • 教えて下さい。m(_ _)m

    翻訳して下さい。 I was asked to perform at the Kentucky Derby, I'm running late I gotta catch a flight it's gonna be a lot of work, and I've got a lot to pack. 宜しくお願いいたします。 m(_ _)m

  • よろしくお願いします

    I am in my early 30s, and a few months ago I moved to a new city where, through a mutual acquaintance, I met a great man who I had a lot of chemistry with. Shortly thereafter he got engaged to his then-girlfriend. I have become relatively close with both of them, and they are both amazing halves of the couple. halves of the coupleとはどういうことでしょうか?よろしくお願いします

  • 代数学★群★英文の問題

    EXAMPLE 8. The additive group of rational numbers is a subgroup of the additive group of real numbers. The group of complex numbers of absolute value 1 is a subgroup of the multiplicatiue group of non-zero complex numbers. The group {1,-1} is a subgroup of {1,-1,i,-i}. という部分群の例題があります。 例題8 (Q,+)≦(R,+) (≦は部分群を示す) A={a+bi|a,b∈R, a^2 + b^2=1} ⊂ C ,C*=C\{0} とすると、A≦(C*,×) {1,-1}≦{1,-1,i,-i} となると思うのですが、和訳はあっているでしょうか? また、1番目の例はわかるのですが 2,3番目の例がどうして部分群かわかりません。 x,y∈H ⇒ xy∈H かつ x^(-1)∈H  ※ x^(-1)はxの逆元 にはどのようにあてはめるのでしょうか? 教えてください。お願いします。

  • 英文を訳して下さい。

    The Battle of Bucharest, also known as the Argeş-Neajlov Defensive Operation in Romania, was an important battle of the Romanian Campaign in World War I, in which the Central Powers occupied the Romanian capital and forced the Romanian Government, as well as the remnants of the Romanian Army to retreat to Moldova and re-establish its capital at Iaşi. The sheer number of troops involved, as well as the large area of operations, make it one of the most complex battles fought on Romanian soil during the war.

  • 英文の和訳をお願いいたします。

    To save us both a lot of time and trouble I will pay the shipping to return the razor to you and ask for a refund of $410USD to my Paypal account. You can keep the other $54USD to cover your time for shipping and handling.

専門家に質問してみよう