Пример #1
0
 /// <summary>
 /// Division outarray[i] / val
 /// </summary>
 public static void Div(Floatarray outarray, float val)
 {
     for (int i = 0; i < outarray.Length1d(); i++)
     {
         outarray.Put1d(i, outarray.At1d(i) / val);
     }
 }
Пример #2
0
 /// <summary>
 /// Subtraction val - outarray[i]
 /// </summary>
 public static void Sub(float val, Floatarray outarray)
 {
     for (int i = 0; i < outarray.Length1d(); i++)
     {
         outarray.Put1d(i, val - outarray.At1d(i));
     }
 }
Пример #3
0
 /// <summary>
 /// Add outarray[i] + val
 /// </summary>
 public static void Add(Floatarray outarray, float val)
 {
     for (int i = 0; i < outarray.Length1d(); i++)
     {
         outarray.UnsafePut1d(i, outarray.UnsafeAt1d(i) + val);
     }
 }
Пример #4
0
        /// <summary>
        /// Normalize the 1-norm of the array.
        /// </summary>
        public static void Normalize1(Floatarray a)
        {
            double scale = 1.0 / Norm1(a);

            for (int i = 0; i < a.Length1d(); i++)
            {
                a.UnsafePut1d(i, (float)(a.UnsafeAt1d(i) * scale));
            }
        }
Пример #5
0
        /// <summary>
        /// 1-norm.
        /// </summary>
        public static double Norm1(Floatarray a)
        {
            double total = 0.0;

            for (int i = 0; i < a.Length1d(); i++)
            {
                total += Math.Abs(a.UnsafeAt1d(i));
            }
            return(total);
        }
Пример #6
0
        public static Floatarray operator -(Floatarray array, float val)
        {
            Floatarray res = array;

            for (int i = 0; i < array.Length1d(); i++)
            {
                res.UnsafePut1d(i, array.UnsafeAt1d(i) - val);
            }
            return(res);
        }
Пример #7
0
        public static Floatarray operator /(Floatarray array, double val)
        {
            Floatarray res = array;

            for (int i = 0; i < array.Length1d(); i++)
            {
                res.UnsafePut1d(i, (float)(array.UnsafeAt1d(i) / val));
            }
            return(res);
        }
Пример #8
0
        /// <summary>
        /// Euclidean norm squared.
        /// </summary>
        public static double Norm2Squared(Floatarray a)
        {
            double total = 0.0;

            for (int i = 0; i < a.Length1d(); i++)
            {
                float value = a.At1d(i);
                total += value * value;
            }
            return(total);
        }
Пример #9
0
        public static void Shuffle <T>(Narray <T> values)
        {
            Floatarray temp  = new Floatarray(values.Length());
            Intarray   index = new Intarray();

            for (int i = 0; i < temp.Length(); i++)
            {
                temp.UnsafePut1d(i, DRandomizer.Default.drand());
            }
            Quicksort(index, temp);
            Permute(values, index);
        }
Пример #10
0
        public static float Fractile(Narray <float> a, double f)
        {
            Floatarray temp = new Floatarray();

            if (!(f >= 0 && f <= 1))
            {
                throw new Exception("CHECK: f >= 0 && f <= 1");
            }
            temp.Copy(a);
            temp.Reshape(temp.Length1d());
            Quicksort(temp);
            return(temp[(int)(f * temp.Length())]);
        }
Пример #11
0
        public static Floatarray operator -(Floatarray outarray1, Floatarray array2)
        {
            if (!SameDims(outarray1, array2))
            {
                throw new Exception("outarray1 and array2 must be same dims!");
            }
            Floatarray res = outarray1;

            for (int i = 0; i < outarray1.Length1d(); i++)
            {
                res.UnsafePut1d(i, outarray1.UnsafeAt1d(i) - array2.UnsafeAt1d(i));
            }
            return(res);
        }
Пример #12
0
        public static float Max(Floatarray a)
        {
            float value = a.At1d(0);

            for (int i = 1; i < a.Length1d(); i++)
            {
                float nvalue = a.At1d(i);
                if (nvalue <= value)
                {
                    continue;
                }
                value = nvalue;
            }
            return(value);
        }
Пример #13
0
        public Floatarray ToFloatarray()
        {
            Floatarray fa = new Floatarray();

            fa.Resize(Width, Height);
            int yput;

            for (int y = 0; y < Height; y++)
            {
                yput = Height - y - 1;
                for (int x = 0; x < Width; x++)
                {
                    fa.Put(x, yput, Convert.ToSingle(Get(y, x) / 255.0f));
                }
            }
            return(fa);
        }
Пример #14
0
        /// <summary>
        /// Euclidean distance squared.
        /// </summary>
        public static double Dist2Squared(Floatarray a, Floatarray b)
        {
            if (!a.SameDims(b))
            {
                throw new Exception("Dist2Squared: !a.SameDims(b)");
            }
            double total = 0.0;

            for (int i = 0; i < a.Length1d(); i++)
            {
                float val = a.At1d(i) - b.At1d(i);
                total += val * val;
            }
            if (double.IsNaN(total))
            {
                throw new Exception("Dist2Squared: total is NaN !");
            }
            return(total);
        }
Пример #15
0
        public static int ArgMin(Floatarray a)
        {
            if (!(/*a.Rank() == 1 && **/ a.Dim(0) > 0))
            {
                throw new Exception("CHECK_ARG: a.Rank()==1 && a.Dim(0)>0");
            }
            float value = a.At1d(0);
            int   index = 0;

            for (int i = 1; i < a.Length1d(); i++)
            {
                float nvalue = a.At1d(i);
                if (nvalue >= value)
                {
                    continue;
                }
                value = nvalue;
                index = i;
            }
            return(index);
        }
Пример #16
0
 /// <summary>
 /// Euclidean norm.
 /// </summary>
 public static double Norm2(Floatarray a)
 {
     return(Math.Sqrt(Norm2Squared(a)));
 }
Пример #17
0
 /// <summary>
 /// Euclidean distance.
 /// </summary>
 public static double Dist2(Floatarray a, Floatarray b)
 {
     return(Math.Sqrt(Dist2Squared(a, b)));
 }