Beispiel #1
0
        internal static double Dot(DoubleArrayVector x, SparseBoolVector y)
        {
            double sum  = 0;
            int    xlen = x.Length;
            int    ylen = y.indices.Length;
            int    i    = 0;
            int    j    = 0;

            while (i < xlen && j < ylen)
            {
                if (i == y.indices[j])
                {
                    j++;
                    sum += x.values[i++];
                }
                else
                {
                    if (i > y.indices[j])
                    {
                        ++j;
                    }
                    else
                    {
                        ++i;
                    }
                }
            }
            return(sum);
        }
Beispiel #2
0
        internal static double Dot(SparseBoolVector x, SparseBoolVector y)
        {
            double sum  = 0;
            int    xlen = x.indices.Length;
            int    ylen = y.indices.Length;
            int    i    = 0;
            int    j    = 0;

            while (i < xlen && j < ylen)
            {
                if (x.indices[i] == y.indices[j])
                {
                    i++;
                    j++;
                    sum += 1;
                }
                else
                {
                    if (x.indices[i] > y.indices[j])
                    {
                        ++j;
                    }
                    else
                    {
                        ++i;
                    }
                }
            }
            return(sum);
        }
Beispiel #3
0
        internal static double SumSquaredDiffs(BoolArrayVector x, SparseBoolVector y)
        {
            double sum  = 0;
            int    xlen = x.Length;
            int    ylen = y.indices.Length;
            int    i    = 0;
            int    j    = 0;

            while (i < xlen && j < ylen)
            {
                if (i == y.indices[j])
                {
                    double d = 1;
                    j++;
                    if (x.values[i++])
                    {
                        d -= 1;
                    }
                    sum += d * d;
                }
                else if (i > y.indices[j])
                {
                    sum += 1;
                    ++j;
                }
                else
                {
                    if (x.values[i])
                    {
                        sum += 1;
                    }
                    ++i;
                }
            }
            while (i < xlen)
            {
                if (x.values[i])
                {
                    sum += 1;
                }
                ++i;
            }
            while (j < ylen)
            {
                sum += 1;
                ++j;
            }
            return(sum);
        }
Beispiel #4
0
 public override double Dot(BaseVector y)
 {
     if (y is SparseFloatVector)
     {
         return(SparseFloatVector.Dot(this, (SparseFloatVector)y));
     }
     if (y is SparseBoolVector)
     {
         return(SparseBoolVector.Dot(this, (SparseBoolVector)y));
     }
     if (y is DoubleArrayVector)
     {
         return(Dot(this, (DoubleArrayVector)y));
     }
     if (y is BoolArrayVector)
     {
         return(BoolArrayVector.Dot((BoolArrayVector)y, this));
     }
     return(Dot(this, (FloatArrayVector)y));
 }
Beispiel #5
0
        internal static double SumSquaredDiffs(SparseBoolVector x, SparseFloatVector y)
        {
            double sum  = 0;
            int    xlen = x.indices.Length;
            int    ylen = y.indices.Length;
            int    i    = 0;
            int    j    = 0;

            while (i < xlen && j < ylen)
            {
                if (x.indices[i] == y.indices[j])
                {
                    double d = 1 - y.values[j++];
                    i++;
                    sum += d * d;
                }
                else if (x.indices[i] > y.indices[j])
                {
                    sum += y.values[j] * y.values[j];
                    ++j;
                }
                else
                {
                    sum += 1;
                    ++i;
                }
            }
            while (i < xlen)
            {
                sum += 1;
                ++i;
            }
            while (j < ylen)
            {
                sum += y.values[j] * y.values[j];
                ++j;
            }
            return(sum);
        }
Beispiel #6
0
        internal static double SumSquaredDiffs(SparseBoolVector x, SparseBoolVector y)
        {
            double sum  = 0;
            int    xlen = x.indices.Length;
            int    ylen = y.indices.Length;
            int    i    = 0;
            int    j    = 0;

            while (i < xlen && j < ylen)
            {
                if (x.indices[i] == y.indices[j])
                {
                    i++;
                    j++;
                }
                else if (x.indices[i] > y.indices[j])
                {
                    sum++;
                    j++;
                }
                else
                {
                    sum++;
                    i++;
                }
            }
            while (i < xlen)
            {
                sum++;
                i++;
            }
            while (j < ylen)
            {
                sum++;
                j++;
            }
            return(sum);
        }
 public override BaseVector Minus(BaseVector other)
 {
     if (other is DoubleArrayVector)
     {
         double[] result = new double[other.Length];
         for (int i = 0; i < result.Length; i++)
         {
             result[i] = -other[i];
         }
         for (int i = 0; i < indices.Length; i++)
         {
             result[indices[i]] += values[i];
         }
         return(new DoubleArrayVector(result));
     }
     if (other is FloatArrayVector)
     {
         float[] result = new float[other.Length];
         for (int i = 0; i < result.Length; i++)
         {
             result[i] = -(float)other[i];
         }
         for (int i = 0; i < indices.Length; i++)
         {
             result[indices[i]] += values[i];
         }
         return(new FloatArrayVector(result));
     }
     if (other is BoolArrayVector)
     {
         float[] result = new float[length];
         for (int i = 0; i < indices.Length; i++)
         {
             result[indices[i]] = values[i];
         }
         BoolArrayVector b = (BoolArrayVector)other;
         for (int i = 0; i < length; i++)
         {
             if (b.values[i])
             {
                 result[i]--;
             }
         }
     }
     if (other is SparseFloatVector)
     {
         SparseFloatVector o          = (SparseFloatVector)other;
         int[]             newIndices = ArrayUtils.UniqueValues(ArrayUtils.Concat(indices, o.indices));
         float[]           newValues  = new float[newIndices.Length];
         for (int i = 0; i < newValues.Length; i++)
         {
             int ind1 = Array.BinarySearch(indices, newIndices[i]);
             if (ind1 >= 0)
             {
                 newValues[i] += values[ind1];
             }
             int ind2 = Array.BinarySearch(o.indices, newIndices[i]);
             if (ind2 >= 0)
             {
                 newValues[i] -= o.values[ind2];
             }
         }
         return(new SparseFloatVector(newIndices, newValues, length));
     }
     if (other is SparseBoolVector)
     {
         SparseBoolVector o          = (SparseBoolVector)other;
         int[]            newIndices = ArrayUtils.UniqueValues(ArrayUtils.Concat(indices, o.indices));
         float[]          newValues  = new float[newIndices.Length];
         for (int i = 0; i < newValues.Length; i++)
         {
             int ind1 = Array.BinarySearch(indices, newIndices[i]);
             if (ind1 >= 0)
             {
                 newValues[i] += values[ind1];
             }
             int ind2 = Array.BinarySearch(o.indices, newIndices[i]);
             if (ind2 >= 0)
             {
                 newValues[i]--;
             }
         }
         return(new SparseFloatVector(newIndices, newValues, length));
     }
     throw new Exception("Never get here.");
 }