5.1 (添字が整数である普通の) 1次元配列

1次元配列は、C言語に(少し)似ている。

real[int] x(3);
これで x[0], x[1], x[2] あるいは x(0), x(1), x(2) が使える。 x[1] でも x(1) でも違いがないのかは不明である。

2次元配列では [ ] は使えないようなので、 ( ) を使う、と覚える方が良いかも。

real[int] a1(3); // Cで double a1[3]; とするのに似ている
for (int i=0;i<3;i++)
  a1[i]=i; // a1(i)=i; としても良い。

real[int] a2 = [0,1,2]; // C で double a[]={0,1,2}; とするのに似てる
real[int] a3 = 0:2; // これは少し MATLAB風

cout << "a1=" << a1 << endl;
cout << "a2=" << a2 << endl;
cout << "a3=" << a3 << endl;

追記: a1 の要素数は a1.n で得られる。

まるで初心者の C++ みたいな

int i,j;
real[int] xx(9),yy(9);
for (i=0; i<9; i++) xx[i]=(i+1);
cout << xx << endl;
for (i=0; i<9; i++) yy(i)=(i+1);
for (i=0; i<9; i++) {
  for (j=0; j<9; j++)
    cout << (xx[i] * yy[j]) << " ";
  cout << endl;
}


実は色々なメンバー関数 (というのかな) が用意されている (次元、最大要素、最小要素、総和、1-ノルム, 2-ノルム, 無限大ノルム)。
operation_of_array.edp

// operation_of_array.edp

int k=10;
real [int] x(k);
for (int i=0; i<k; i++)
  x(i)=i;
cout << "x=" << x << endl;

cout << "length of x=" << x.n << endl;
for (int i=0; i<k; i++)
  cout << "x(" << i << ")=" << x(i) << endl;

cout << "max of x=" << x.max << endl;
cout << "min of x=" << x.min << endl;
cout << "sum of x=" << x.sum << endl;
cout << "l1 norm of x=" << x.l1 << endl;
cout << "l2 norm of x=" << x.l2 << endl;
cout << "infinity norm of x=" << x.linfty << endl;
実行結果
x=10
	  0	  1	  2	  3	  4
	  5	  6	  7	  8	  9

length of x=10
x(0)=0
x(1)=1
x(2)=2
x(3)=3
x(4)=4
x(5)=5
x(6)=6
x(7)=7
x(8)=8
x(9)=9
max of x=9
min of x=0
sum of x=45
l1 norm of x=45
l2 norm of x=16.8819
infinity norm of x=9



桂田 祐史