⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 array.sl

📁 一个C格式的脚本处理函数库源代码,可让你的C程序具有执行C格式的脚本文件
💻 SL
📖 第 1 页 / 共 2 页
字号:
  failed ("A[3,7]=NULL");A = String_Type[10];A[*] = "a";A[1] = NULL;if (length (where (A != String_Type[10])) != 9)  failed ("A != String_Type[10]");% Test array summing operations#ifexists Double_Typestatic define compute_sum (a, n){   variable s = 0;   variable b;   variable i, j, k;   variable dims;      (dims,,) = array_info (a);   if (n == 0)     {	b = Double_Type[dims[1],dims[2]];	for (i = 0; i < dims[1]; i++)	  {	     for (j = 0; j < dims[2]; j++)	       {		  for (k = 0; k < dims[n]; k++)		    b[i,j] += a[k,i,j];	       }	  }	return b;     }   if (n == 1)     {	b = Double_Type[dims[0],dims[2]];	for (i = 0; i < dims[0]; i++)	  {	     for (j = 0; j < dims[2]; j++)	       {		  for (k = 0; k < dims[n]; k++)		    b[i,j] += a[i,k,j];	       }	  }	return b;     }   if (n == 2)     {	b = Double_Type[dims[0],dims[1]];	for (i = 0; i < dims[0]; i++)	  {	     for (j = 0; j < dims[1]; j++)	       {		  for (k = 0; k < dims[n]; k++)		    b[i,j] += a[i,j,k];	       }	  }	return b;     }      b = 0.0;   for (i = 0; i < dims[0]; i++)     {	for (j = 0; j < dims[1]; j++)	  {	     for (k = 0; k < dims[2]; k++)	       b += a[i,j,k];	  }     }   return b;}A = [1:3*4*5];reshape (A, [3,4,5]);define test_sum (a, n){   variable s1, s2;   if (n == -1)     s1 = sum(A);   else     s1 = sum(A,n);      s2 = compute_sum (A, n);   if (neqs (s1, s2))     {	failed ("sum(A,%d): %S != %S: %g != %g", n, s1, s2, s1[0,0], s2[0,0]);     }}test_sum (A,-1);test_sum (A,2);test_sum (A,1);test_sum (A,0);A = [1+2i, 2+3i, 3+4i];if (sum(A) != A[0] + A[1] + A[2])  failed ("sum(Complex)");#endif				       %  Double_Typedefine find_min (a){   variable m = a[0];   _for (1, length(a)-1, 1)     {	variable i = ();	if (a[i] < m)	  m = a[i];     }   return m;}define find_max (a){   variable m = a[0];   _for (1, length(a)-1, 1)     {	variable i = ();	if (a[i] > m)	  m = a[i];     }   return m;}define test_eqs (what, a, b){   if (_typeof(a) != _typeof(b))     failed ("%s: %S != %S", what, a, b);      if (neqs (a, b))     failed ("%s: %S != %S", what, a, b);}A = [1:10];test_eqs ("min", min(A), find_min(A));test_eqs ("max", max(A), find_max(A));#ifexists Double_TypeA *= 1.0f;test_eqs ("min", min(A), find_min(A));test_eqs ("max", max(A), find_max(A));A *= 1.0;test_eqs ("min", min(A), find_min(A));test_eqs ("max", max(A), find_max(A));#endifA = [1h:10h];test_eqs ("min", min(A), find_min(A));test_eqs ("max", max(A), find_max(A));A = ['0':'9'];test_eqs ("min", min(A), find_min(A));test_eqs ("max", max(A), find_max(A));A=Int_Type[10,10];A[*,*] = [0:99];if (length (A[[0:99:11]]) != 10)  failed ("A[[0:99:11]");#ifexists cumsumstatic define do_cumsum (a){   variable b = 1.0 * a;   variable i, s;      s = 0;   _for (0, length(a)-1, 1)     {	i = ();	s += a[i];	b[i] = s;     }   return b;}   static define test_cumsum (a, k, result_type){   variable b = 1.0 * a;   variable bb;   variable dims, ndims;   variable i, j;   (dims, ndims, ) = array_info (a);   if (k != -1)     bb = cumsum (a, k);   else     bb = cumsum (a);   if (_typeof (bb) != result_type)     {	failed ("cumsum(%S) has wrong return type (%S)", a, b);     }#ifexists Complex_Type   if ((_typeof (a) != Complex_Type) and (_typeof (a) != Float_Type))#endif     a = typecast (a, Double_Type);   if (k == -1)     {	b = do_cumsum (_reshape (a, [length(a)]));     }   else switch (ndims)     {      case 1:	b = cumsum (a);     }     {      case 2:	if (k == 0)	  {	     %a_j = cumsum_i a_ij	     _for (0, dims[1]-1, 1)	       {		  j = ();		  b[*, j] = do_cumsum (a[*, j]);	       }	  }	else	  {	     _for (0, dims[1]-1, 1)	       {		  i = ();		  b[i, *] = do_cumsum (a[i, *]);	       }	  }     }     {      case 3:	if (k == 0)	  {	     %a_j = cumsum_i a_ij	     _for (0, dims[1]-1, 1)	       {		  i = ();		  _for (0, dims[2]-1, 1)		    {		       j = ();		       b[*, i, j] = do_cumsum (a[*, i, j]);		    }	       }	  }	else if (k == 1)	  {	     _for (0, dims[0]-1, 1)	       {		  i = ();		  _for (0, dims[2]-1, 1)		    {		       j = ();		       b[i, *, j] = do_cumsum (a[i, *, j]);		    }	       }	  }	else	  {	     _for (0, dims[0]-1, 1)	       {		  i = ();		  _for (0, dims[1]-1, 1)		    {		       j = ();		       b[i, j, *] = do_cumsum (a[i, j, *]);		    }	       }	  }     }      if (neqs (b, bb))     {	failed ("cumsum (%S, %d), expected %S, got %S", a, k, b, bb);     }}A = Int_Type[10]; A[*] = 1;test_cumsum (A, -1, Double_Type);test_cumsum (A, 0, Double_Type);A = [1:3*4*5];reshape (A, [3,4,5]);test_cumsum (A, -1, Double_Type);test_cumsum (A, 0, Double_Type);test_cumsum (A, 1, Double_Type);test_cumsum (A, 2, Double_Type);A = Char_Type[10]; A[*] = 1;test_cumsum (A, -1, Float_Type);test_cumsum (A, 0, Float_Type);A = [1:3*4*5]; A = typecast (A, Char_Type);reshape (A, [3,4,5]);test_cumsum (A, -1, Float_Type);test_cumsum (A, 0, Float_Type);test_cumsum (A, 1, Float_Type);test_cumsum (A, 2, Float_Type);A = UChar_Type[10]; A[*] = 1;test_cumsum (A, -1, Float_Type);test_cumsum (A, 0, Float_Type);A = [1:3*4*5]; A = typecast (A, UChar_Type);reshape (A, [3,4,5]);test_cumsum (A, -1, Float_Type);test_cumsum (A, 0, Float_Type);test_cumsum (A, 1, Float_Type);test_cumsum (A, 2, Float_Type);A = Short_Type[10]; A[*] = 1;test_cumsum (A, -1, Float_Type);test_cumsum (A, 0, Float_Type);A = [1:3*4*5]; A = typecast (A, Short_Type);reshape (A, [3,4,5]);test_cumsum (A, -1, Float_Type);test_cumsum (A, 0, Float_Type);test_cumsum (A, 1, Float_Type);test_cumsum (A, 2, Float_Type);A = UShort_Type[10]; A[*] = 1;test_cumsum (A, -1, Float_Type);test_cumsum (A, 0, Float_Type);A = [1:3*4*5]; A = typecast (A, UShort_Type);reshape (A, [3,4,5]);test_cumsum (A, -1, Float_Type);test_cumsum (A, 0, Float_Type);test_cumsum (A, 1, Float_Type);test_cumsum (A, 2, Float_Type);A = Float_Type[10]; A[*] = 1;test_cumsum (A, -1, Float_Type);test_cumsum (A, 0, Float_Type);A = [1:3*4*5]*1.0f;reshape (A, [3,4,5]);test_cumsum (A, -1, Float_Type);test_cumsum (A, 0, Float_Type);test_cumsum (A, 1, Float_Type);test_cumsum (A, 2, Float_Type);#ifexists Complex_TypeA = Complex_Type[10]; A[*] = 1;test_cumsum (A, -1, Complex_Type);test_cumsum (A, 0, Complex_Type);A = [1:3*4*5] + 2i*[1:3*4*5];reshape (A, [3,4,5]);test_cumsum (A, -1, Complex_Type);test_cumsum (A, 0, Complex_Type);test_cumsum (A, 1, Complex_Type);test_cumsum (A, 2, Complex_Type);#endif#endifprint ("Ok\n");exit (0);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -