[ 簡単な説明 ] 複素数ライブラリの使用例です。 本ライブラリは、各関数の処理結果が戻り値となるように作られていますので、ダミー変数を使用する必要がありません。 ( 例えば、x = a × b + c ÷ d の計算をする場合、各関数が結果を引数に与えるようにしていると、 cmul(a, b, &w1); cdiv(c, d, &w2); cadd(w1, w2, &x); のようにコーディングしなければなりませんが、本ライブラリでは、 x = cadd( cmul(a, b), cdiv(c, d) ); としてコーディングできます。) |
/* test1.c */ #include <stdio.h> #include "sslib.h" void cdisp(Complex a); void cdisp(Complex a) { printf("(%22.15e, %22.15e)\n", a.r, a.i); } int main(void) { Complex a, b, c, k[3]; a = tocomplex(2., 2.); b = tocomplex(1., 1.); c = tocomplex(1., 1.); printf("a = "); cdisp(a); printf("b = "); cdisp(b); printf("c = "); cdisp(c); printf("a == b ? %d\n", ceq(a, b)); printf("b == c ? %d\n", ceq(b, c)); printf("a != b ? %d\n", cne(a, b)); printf("b != c ? %d\n", cne(b, c)); printf("conjugate number of a = "); cdisp(conj(a)); printf("a + b = "); cdisp(cadd(a, b)); printf("a - b = "); cdisp(csub(a, b)); printf("a * b = "); cdisp(cmul(a, b)); printf("a / b = "); cdisp(cdiv(a, b)); printf("abs(a) = %22.15e\n", cabslt(a)); printf("abs(b) = %22.15e\n", cabslt(b)); printf("arg(a) = %22.15e\n", carg(a)); printf("arg(b) = %22.15e\n", carg(b)); printf("ln(a) = "); cdisp(cln(a)); printf("log10(a) = "); cdisp(clog10(a)); printf("exp(a) = "); cdisp(cexp(a)); printf("a ^ b = "); cdisp(cpow(a, b)); printf("a ^ 2.5 = "); cdisp(cpowd(a, 2.5)); printf("sin(a) = "); cdisp(csin(a)); printf("cos(a) = "); cdisp(ccos(a)); printf("tan(a) = "); cdisp(ctan(a)); printf("sqr(a) = "); cdisp(csqrt(a)); k[0] = tocomplex(1., 0.); k[1] = tocomplex(2., 1.); k[2] = tocomplex(3., 2.); printf("k0 = "); cdisp(k[0]); printf("k1 = "); cdisp(k[1]); printf("k2 = "); cdisp(k[2]); printf("f(x) = k0 * x * x + k1 * x + k2\n"); printf("f(a) = "); cdisp(cfunc(k, 2, a)); printf("f(b) = "); cdisp(cfunc(k, 2, b)); return 1; } |
a = ( 2.000000000000000e+00, 2.000000000000000e+00) b = ( 1.000000000000000e+00, 1.000000000000000e+00) c = ( 1.000000000000000e+00, 1.000000000000000e+00) a == b ? 0 b == c ? 1 a != b ? 1 b != c ? 0 conjugate number of a = ( 2.000000000000000e+00, -2.000000000000000e+00) a + b = ( 3.000000000000000e+00, 3.000000000000000e+00) a - b = ( 1.000000000000000e+00, 1.000000000000000e+00) a * b = ( 0.000000000000000e+00, 4.000000000000000e+00) a / b = ( 2.000000000000000e+00, 0.000000000000000e+00) abs(a) = 2.828427124746190e+00 abs(b) = 1.414213562373095e+00 arg(a) = 7.853981633974483e-01 arg(b) = 7.853981633974483e-01 ln(a) = ( 1.039720770839918e+00, 7.853981633974483e-01) log10(a) = ( 4.515449934959718e-01, 3.410940884604603e-01) exp(a) = (-3.074932320639359e+00, 6.718849697428250e+00) a ^ b = (-3.244472096893367e-01, 1.248106813910933e+00) a ^ 2.5 = (-5.148754023244662e+00, 1.243019179224030e+01) sin(a) = ( 3.420954861117013e+00, -1.509306485323616e+00) cos(a) = (-1.565625835315743e+00, -3.297894836311237e+00) tan(a) = (-2.839295286823225e-02, 1.023835594570473e+00) sqr(a) = ( 1.553773974030037e+00, 6.435942529055827e-01) k0 = ( 1.000000000000000e+00, 0.000000000000000e+00) k1 = ( 2.000000000000000e+00, 1.000000000000000e+00) k2 = ( 3.000000000000000e+00, 2.000000000000000e+00) f(x) = k0 * x * x + k1 * x + k2 f(a) = ( 5.000000000000000e+00, 1.600000000000000e+01) f(b) = ( 4.000000000000000e+00, 7.000000000000000e+00) |