Startpage >> Main >> SingleRealArrayWithIntegerIndices

Single Real Array With Integer Indices

Test global attributes of arrays, like array.sum, array.max, array.n, resizing array, the a?b:c operation, the sorting and the index mapping

download this example: singlearrays1.edp or return to Matrices and Arrays

real[int] tab(5),tab1(8);
tab=1.03;
tab[2]=3;
cout << " array tab = " << tab << endl;
cout << " size of tab = " << tab.n << endl;
cout << " minimum of tab = " << tab.min << endl;
cout << " maximum of tab = " << tab.max << endl;
cout << " sum of tab = " << tab.sum << endl;
cout << " ||tab||_1  = "<<tab.l1<<endl;
cout << " ||tab||_2  = "<<tab.l2<<endl;
cout << " ||tab||_infty = " <<tab.linfty<<endl;
cout << " sum tab_i  = "<<tab.sum<<endl;
cout << " tab'*tab  = "<< tab'*tab <<endl;
cout<<"tab quantile 0.2 = " << tab.quantile(0.2) << endl;

tab.resize(8);
cout << " resized array tab filled with zeros= " << tab << endl;
tab(5:8)=10;
cout << " resized array tab = " << tab << endl;
tab.resize(6);
cout << " reduced array tab = " << tab << endl;

real[int] a(5),b(5),c(5),d(5); 
a = 1; 
b = 2;
c = 3; 
a[2]=0; 
a[4]=0;
d = ( a ? b : c );
cout<<"d=(a?b:c) is as follows:  "<<endl;
cout<<"if a(i) non-cero set d(i)=b(i)"<<endl;  
cout<<"if a(i) cero set d(i)=c(i)"<<endl;
cout<<"d=(a?b:c) is "<<d<<endl;
d = ( a ? 1 : c );
cout<<"d=(a?1:c) is "<<d<<endl;
d = ( a ? b : 0 );
cout<<"d=(a?b:0) is "<<d<<endl;
d = ( a ? 1 : 0 );
cout<<"d=(a?1:0) is "<<d<<endl;
cout << " array tab = " << tab << endl;
tab.sort;
cout << " sorted array tab = " << tab << endl;
//
cout<<"++++++++ Sorting in parallel example   ++++++"<<endl;
int[int] ii(0:d.n-1); 
d=-1:-5;
d[3]=2;
d[0]=1;
cout << " ii " << ii<<endl;
cout << " d " << d<<endl;
cout<<"We can sort arrays d and ii in paralel"<<endl;
sort(d,ii); 
cout << " d " << d << "\n ii = " << ii << endl;
//
// sorting according to a set of indices...I(j) negative do nothing
//
cout<<"++++++++ Index mapping example   ++++++"<<endl;

int[int] I=[2,3,4,-1,0];
b=c=-3;
cout<<"++++++++ Index I(j) negative do nothing   ++++++"<<endl;
cout << "index I : I= "<<I<<endl; 
cout << "array a : a = "<<a<<endl;
cout << "array b=c : b = "<<b<<endl;
cout<< "b=a(I) set b(j)=a(I(j)) j=0,1,2,...,b.n-1 "<<endl;
b=a(I);
cout<< "c(I)=a set c(I(j))=a(j) j=0,1,2,...,I.n-1 "<<endl;
c(I)=a;
cout<<" b = a(I) size "<<b<< "\n c(I)=a size "<<c<<endl;

return to Matrices and Arrays

Page last modified on April 03, 2014, at 01:03 PM