Exemplo n.º 1
0
 public virtual void CoerceZero(double epsilon)
 {
     for (var i = 0; i < this.Rows; i++)
     {
         for (var j = 0; j < this.Columns; j++)
         {
             if (CommonUtil.Equals(this.GetAt(i, j), 0, epsilon))
             {
                 this.SetAt(i, j, 0f);
             }
         }
     }
 }
        public static bool operator !=(VectorNd left, VectorNd right)
        {
            var dim = Math.Max(left.Dimension, right.Dimension);

            for (var i = 0; i < dim; i++)
            {
                if (!CommonUtil.Equals(left[i], right[i]))
                {
                    return(true);
                }
            }

            return(false);
        }
        public override bool Equals(object obj)
        {
            var vo = obj as VectorNd?;

            if (!vo.HasValue)
            {
                return(false);
            }

            var v   = vo.Value;
            var dim = Math.Max(this.Dimension, v.Dimension);

            for (var i = 0; i < dim; i++)
            {
                if (CommonUtil.Equals(this[i], v[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
 public static bool operator !=(Vector4d left, Vector4d right)
 {
     return(!CommonUtil.Equals(left.X, right.X) || !CommonUtil.Equals(left.Y, right.Y) || !CommonUtil.Equals(left.Z, right.Z) || !CommonUtil.Equals(left.W, right.W));
 }
 public static bool operator ==(Vector4d left, Vector4d right)
 {
     return(CommonUtil.Equals(left.X, right.X) && CommonUtil.Equals(left.Y, right.Y) && CommonUtil.Equals(left.Z, right.Z) && CommonUtil.Equals(left.W, right.W));
 }
        public MatrixF Invert()
        {
            var result   = (Matrix4x4f)this.Clone();
            var indices1 = new int[4];
            var indices2 = new int[4];
            var indices3 = new[] { -1, -1, -1, -1 };

            var a = 0;
            var b = 0;

            for (var i = 0; i < 4; ++i)
            {
                var value = 0.0f;
                for (var j = 0; j < 4; ++j)
                {
                    if (indices3[j] == 0)
                    {
                        continue;
                    }

                    for (var k = 0; k < 4; ++k)
                    {
                        if (indices3[k] == -1)
                        {
                            var num2 = Math.Abs(result.GetAt(j, k));
                            if (num2 <= value)
                            {
                                continue;
                            }

                            value = num2;
                            b     = j;
                            a     = k;
                        }
                        else if (indices3[k] > 0)
                        {
                            return(result);
                        }
                    }
                }

                ++indices3[a];
                if (b != a)
                {
                    for (var j = 0; j < 4; ++j)
                    {
                        var num2 = result.GetAt(b, j);
                        result.SetAt(b, j, result.GetAt(a, j));
                        result.SetAt(a, j, num2);
                    }
                }

                indices2[i] = b;
                indices1[i] = a;

                var temp = result.GetAt(a, a);
                if (CommonUtil.Equals(temp, 0.0f))
                {
                    throw new InvalidOperationException("MatrixUtil is singular and cannot be inverted.");
                }

                var invTemp = 1.0f / temp;
                result.SetAt(a, a, 1.0f);

                for (var j = 0; j < 4; ++j)
                {
                    result[a, j] *= invTemp;
                }

                for (var j = 0; j < 4; ++j)
                {
                    if (a == j)
                    {
                        continue;
                    }

                    var scalar = result.GetAt(j, a);
                    result.SetAt(j, a, 0.0f);
                    for (var k = 0; k < 4; ++k)
                    {
                        result[j, k] -= result.GetAt(a, k) * scalar;
                    }
                }
            }

            for (var i = 3; i >= 0; --i)
            {
                var switchInd = indices2[i];
                var otherInd  = indices1[i];
                for (var j = 0; j < 4; ++j)
                {
                    var num = result.GetAt(j, switchInd);
                    result.SetAt(j, switchInd, result.GetAt(j, otherInd));
                    result.SetAt(j, otherInd, num);
                }
            }

            return(result);
        }