Esempio n. 1
0
        internal static double Dot(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])
                {
                    sum += y.values[j++];
                    i++;
                }
                else
                {
                    if (x.indices[i] > y.indices[j])
                    {
                        ++j;
                    }
                    else
                    {
                        ++i;
                    }
                }
            }
            return(sum);
        }
Esempio n. 2
0
        internal static double Dot(DoubleArrayVector x, SparseFloatVector 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])
                {
                    sum += x.values[i++] * y.values[j++];
                }
                else
                {
                    if (i > y.indices[j])
                    {
                        ++j;
                    }
                    else
                    {
                        ++i;
                    }
                }
            }
            return(sum);
        }
 public override MatrixIndexer ExtractColumns(IList<int> columns)
 {
     SparseFloatVector[] r = new SparseFloatVector[vals.Length];
     for (int i = 0; i < vals.Length; i++){
         r[i] = (SparseFloatVector) vals[i].SubArray(columns);
     }
     return new SparseRowFloatMatrixIndexer{vals = r, ncolumns = columns.Count};
 }
 public override object Clone()
 {
     if (vals == null){
         return new SparseRowFloatMatrixIndexer();
     }
     SparseFloatVector[] v = new SparseFloatVector[vals.Length];
     for (int i = 0; i < v.Length; i++){
         v[i] = (SparseFloatVector) vals[i].Clone();
     }
     return new SparseRowFloatMatrixIndexer{vals = v, ncolumns = ncolumns};
 }
Esempio n. 5
0
        internal static double SumSquaredDiffs(BoolArrayVector x, SparseFloatVector 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 = y.values[j++];
                    if (x.values[i++])
                    {
                        d -= 1;
                    }
                    sum += d * d;
                }
                else if (i > y.indices[j])
                {
                    sum += y.values[j] * y.values[j];
                    ++j;
                }
                else
                {
                    if (x.values[i])
                    {
                        sum += 1;
                    }
                    ++i;
                }
            }
            while (i < xlen)
            {
                if (x.values[i])
                {
                    sum += 1;
                }
                ++i;
            }
            while (j < ylen)
            {
                sum += y.values[j] * y.values[j];
                ++j;
            }
            return(sum);
        }
Esempio n. 6
0
 public override double SumSquaredDiffs(BaseVector y)
 {
     if (y is SparseFloatVector)
     {
         return(SparseFloatVector.SumSquaredDiffs(this, (SparseFloatVector)y));
     }
     if (y is DoubleArrayVector)
     {
         return(SumSquaredDiffs(this, (DoubleArrayVector)y));
     }
     if (y is BoolArrayVector)
     {
         return(BoolArrayVector.SumSquaredDiffs((BoolArrayVector)y, this));
     }
     return(SumSquaredDiffs(this, (FloatArrayVector)y));
 }
Esempio n. 7
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));
 }
Esempio n. 8
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);
        }
Esempio n. 9
0
 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.");
 }
 public SparseRowFloatMatrixIndexer(SparseFloatVector[] vals, int ncolumns)
 {
     this.vals = vals;
     this.ncolumns = ncolumns;
 }
 public override void Set(float[,] value)
 {
     ncolumns = value.GetLength(1);
     vals = new SparseFloatVector[value.GetLength(0)];
     for (int i = 0; i < vals.Length; i++){
         List<int> v = new List<int>();
         for (int j = 0; j < ncolumns; j++){
             if (value[i, j] == 0){
                 continue;
             }
             v.Add(j);
         }
         int[] v1 = v.ToArray();
         float[] x = new float[v1.Length];
         for (int j = 0; j < v1.Length; j++){
             x[j] = value[i, v1[j]];
         }
         vals[i] = new SparseFloatVector(v1, x, ncolumns);
     }
 }
 public override void Init(int nrows, int ncolumns1)
 {
     ncolumns = ncolumns1;
     vals = new SparseFloatVector[nrows];
     for (int i = 0; i < nrows; i++){
         vals[i] = new SparseFloatVector(new int[0], new float[0], ncolumns);
     }
 }
Esempio n. 13
0
 internal static double SumSquaredDiffs(BoolArrayVector x, SparseFloatVector 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 = y.values[j++];
             if (x.values[i++]){
                 d -= 1;
             }
             sum += d*d;
         } else if (i > y.indices[j]){
             sum += y.values[j]*y.values[j];
             ++j;
         } else{
             if (x.values[i]){
                 sum += 1;
             }
             ++i;
         }
     }
     while (i < xlen){
         if (x.values[i]){
             sum += 1;
         }
         ++i;
     }
     while (j < ylen){
         sum += y.values[j]*y.values[j];
         ++j;
     }
     return sum;
 }
Esempio n. 14
0
 internal static double SumSquaredDiffs(SparseFloatVector 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 = x.values[i++] - y.values[j++];
             sum += d*d;
         } else if (x.indices[i] > y.indices[j]){
             sum += y.values[j]*y.values[j];
             ++j;
         } else{
             sum += x.values[i]*x.values[i];
             ++i;
         }
     }
     while (i < xlen){
         sum += x.values[i]*x.values[i];
         ++i;
     }
     while (j < ylen){
         sum += y.values[j]*y.values[j];
         ++j;
     }
     return sum;
 }
Esempio n. 15
0
 internal static double Dot(DoubleArrayVector x, SparseFloatVector 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]){
             sum += x.values[i++]*y.values[j++];
         } else{
             if (i > y.indices[j]){
                 ++j;
             } else{
                 ++i;
             }
         }
     }
     return sum;
 }