00001 using System;
00002
00003 namespace FFT
00004 {
00005
00006
00007
00008
00009 class Utils
00010 {
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 public static T[] copy<T>(T[] src, T[] dst)
00025 {
00026 for (int i = 0; i < src.Length && i < dst.Length; i++) {
00027 dst[i] = src[i];
00028 }
00029
00030 return dst;
00031 }
00032
00033
00034
00035
00036
00037
00038
00039 public static double dist(Complex[] a, Complex[] b)
00040 {
00041 double norm = 0;
00042 int n = Math.Min(a.Length,b.Length);
00043
00044 for (int i = 0; i < n; i++) {
00045 norm += Math.Pow((a[i]-b[i]).Modulus,2);
00046 }
00047
00048 return Math.Sqrt(norm);
00049 }
00050
00051
00052
00053
00054
00055
00056
00057
00058 public static int Log2(int n)
00059 {
00060 if (n <= 0) {
00061 return 0;
00062 } else {
00063 int i = 0;
00064 int m = n;
00065 while (m > 0) {
00066 m >>= 1;
00067 i++;
00068 }
00069
00070 return (IsPowerOf2(n)) ? (i-1) : (i);
00071 }
00072 }
00073
00074
00075
00076
00077 public static int Exp2(int n)
00078 {
00079 return 1 << n;
00080 }
00081
00082
00083
00084
00085 public static bool IsPowerOf2(int n)
00086 {
00087 return (n & (n - 1)) == 0;
00088 }
00089
00090
00091
00092
00093
00094
00095
00096 public static void PrintArray<T>(T[] t, params string[] name)
00097 {
00098 if (t != null) {
00099 Console.Write(t.GetType().GetElementType().Name);
00100 } else {
00101 Console.Write("unknown");
00102 }
00103
00104 Console.Write(" [] ");
00105
00106 if (name.Length != 0) {
00107 Console.Write(name[0]);
00108 }
00109
00110 Console.Write(" = ");
00111
00112 if (t == null) {
00113 Console.Write("null\n");
00114 return;
00115 }
00116
00117 Console.Write("[");
00118 for (int i = 0; i < t.Length; i++) {
00119 Console.Write(t[i]);
00120 if (i < t.Length - 1) {
00121 Console.Write(",");
00122 }
00123 }
00124 Console.Write("]\n");
00125
00126 }
00127 }
00128 }