LoginSignup
11
11

C#の複素数クラスは便利

Last updated at Posted at 2023-11-14

C#での複素数クラス

 単純な複素数クラスなら自分で実装できるが、算術関数も実装するとなると、大変そうであるので、ライブラリ等を利用したい。
java標準にはないので、Processing標準からは手段がない?
pythonモードで複素数を扱ことが可能であり、過去の記事を貼っておく。

2024/04/14追記:いいライブラリがあったので、記事にしたものを貼っておく。

複素数の計算

二乗してみます。

i^2=-1
using System.Numerics;
Complex c = new Complex(0, 1);
Console.WriteLine(Complex.Multiply(c, c));
<-1; 0>

しかし、powを使うとコンピュータの精度問題により、僅かな誤差が生じる。
精度は、double相当の約16桁?

using System.Numerics;
Complex c = new Complex(0, 1);
Console.WriteLine(Complex.Pow(c, 2));
<-1; 1.2246467991473532E-16>

次に、sqrt命令

\sqrt{i}=\frac{1}{\sqrt{2}}+\frac{1}{\sqrt{2}}i
using System.Numerics;
Complex c = new Complex(0, 1);
Console.WriteLine(Complex.Sqrt(c));
<0.7071067811865476; 0.7071067811865475>

指数部に実数0.5を使う。

using System.Numerics;
Complex c = new Complex(0, 1);
Console.WriteLine(Complex.Pow(c, 0.5));
<0.7071067811865476; 0.7071067811865475>

まったく同じ値となった。sqrtは内部でpowに置き換えられてるのかな?

これで、マンデルブロ集合の指数部を2以外に変更する時のコードがシンプルに書けるはず。

さて、虚数の虚数乗は?

i^i=e^{-\pi/2}=\frac{1}{\sqrt{e^\pi}}=0.20787957\cdots
using System.Numerics;
Complex c = new Complex(0, 1);
Console.WriteLine(Complex.Pow(c, c));
<0.20787957635076193; 0>

完璧である

計算速度は遅い

便利であるが、期待してはいけない。
ちょっとなら使えるが、ゴリゴリ計算するのには向いてない。

using System.Numerics;

for (double y = -1.25d; y <= 1.25d; y += 2.5d/24.0d)
{
    for (double x = -2.0d; x <= 0.5d; x += 2.5d/64.0d)
    {
        Complex z = new Complex(0, 0);
        Complex c = new Complex(x, y);
        for (int n = 0; n < 256; n++)
        {
            z = Complex.Pow(z, 2);
            z = Complex.Add(z, c);

            if (z.Magnitude > 2)
            {
                Console.Write(".");
                break;
            }
            if (n == 255)
            {
                Console.Write("*");
            }
        }
    }
    Console.WriteLine("");
}
.................................................................
.................................................................
.................................................................
.................................................................
................................................*................
..............................................*****..............
..........................................*.*********.*..........
.......................................*********************.....
...................................*************************.....
..................................****************************...
......................********...*****************************...
....................************.****************************....
......*........*******************************************.......
....................************.****************************....
......................********...*****************************...
..................................****************************...
...................................*************************.....
.......................................*********************.....
..........................................*.*********.*..........
..............................................*****..............
................................................*................
.................................................................
.................................................................
.................................................................
.................................................................

漸化式が、z^2+cではなくて、z^2.2+cにしたいとかなら、簡潔に書ける。

................................................................
................................................................
................................................................
......................................................*.........
.............................................*.*.....***........
.........................*******.........***************........
.....................*************...**********************.....
........................*************************************...
........................*.************************************..
................................******************************..
.............................*********************************..
............................*******************************.....
.............................*********************************..
................................******************************..
........................*.************************************..
........................*************************************...
.....................*************...**********************.....
.........................*******.........***************........
.............................................*.*.....***........
......................................................*.........
................................................................
................................................................
................................................................

image.png

z^z+cはどうだろう?

............**********************************************......
..........************************************************......
.........**************************************************.....
.......****************************************************.....
......******************************************************....
.....*******************************.***********************....
....********************************.************************...
.....********************************************************...
......**********.....*************.**************************...
.......*************....***********************************.....
.........*************************************************......
...........***********.**..*****************************........
..............******.*******************************............
...........***********.**..*****************************........
.........*************************************************......
.......*************....***********************************.....
......**********.....*************.**************************...
.....********************************************************...
....********************************.************************...
.....*******************************.***********************....
......******************************************************....
.......****************************************************.....
.........**************************************************.....
..........************************************************......

やりたいことのメモ

マンデルブロの中身の着色
より多い桁数の楽な扱い方法

その他

マイクロソフトのページ

Complex value = new Complex(12, -6);
for (int power = -1; power <= 10; power++)
  Console.WriteLine("{0} ^ {1,2} = {2:N2}", value, power, Complex.Pow(value, power));

この意味が、理解しにくかったのでメモ。
まず、""の中、
{0}はvalue
{1,2}はpower
{2:N2}はComplex.Pow(value, power)
が入る。

{1,2}の「,2」は「2桁表示の右寄せ」という意味。
{1,-3}の場合「3桁表示の左寄せ」になる。
※昔のように、スペースを挿入する技は必要はない。
{2:N2}の「:N2」は小数点以下2桁で表示という意味。
以上は、string.Formatメソッドに準拠している。

main関数なくても良くなった

Visual Studio 2022のバージョンでは、main関数書かなくても動くギミックが実装された。気づかずに、書いてたけど、気づかないほど、自然な良い機能であるといえる。
※時期が違うかもしれない。macなので、表記がなくてよくわからない。

i^i^i^i^i^i= ?

(顔文字ではない)
Google検索ボックスで虚数扱えるので、興味本位に計算した。

i^{i^i}=0.947158998+0.32076445i
i^{i^{i^i}}=0.0500922361+0.602116527i
i^{i^{i^{i^i}}}=0.387166181+0.0305270816i
i^{i^{i^{i^{i^i}}}}=0.782275682+0.544606558i

0.142561823 + 0.400466525 i
0.519786396 + 0.118384196 i
0.568588617 + 0.605078407 i
0.242365247 + 0.301150592 i
10回目まで計算してみた。
複素数の積は、複素平面上の回転を表すが、冪乗は何を表すのだろうか?・・・回転のようですね。
極限は、
0.438283 + 0.3605924 i
とのこと。

image.png

参考

最近のCPUはfloatよりもdoubleが速いらしい。マジか。

iのi乗

ランベルトのW関数

マンデルブロ集合

マンデルブロ集合×反復関数系

11
11
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11