Beispiel #1
0
 /// <summary>
 /// Concatenate the elements of vectors to the current vector.
 /// </summary>
 /// <param name="vector"></param>
 public void AddComponents(QsVector vector)
 {
     foreach (var s in vector)
     {
         ListStorage.Add(s);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Form Vector From QsValues that are scalars or vector.
        /// </summary>
        /// <param name="values">Scalars</param>
        /// <returns></returns>
        public static QsValue VectorFromValues(params QsValue[] values)
        {
            QsVector vec = new QsVector(values.Length);

            foreach (var val in values)
            {
                if (val is QsScalar)
                {
                    vec.AddComponent((QsScalar)val);
                }
                else if (val is QsVector)
                {
                    var vc = val as QsVector;
                    foreach (var component in vc)
                    {
                        vec.AddComponent(component);
                    }
                }
                else
                {
                    throw new QsException("Component is not a scalar or vector value.");
                }
            }

            return(vec);
        }
Beispiel #3
0
        /// <summary>
        /// Copy the vector to another instance with the same components.
        /// </summary>
        /// <param name="vector"></param>
        /// <returns></returns>
        public static QsVector CopyVector(QsVector vector)
        {
            QsVector vec = new QsVector(vector.Count);

            foreach (var q in vector)
            {
                vec.AddComponent((QsScalar)q.Clone());
            }
            return(vec);
        }
Beispiel #4
0
        public QsVector PowerScalar(QsScalar scalar)
        {
            QsVector v = new QsVector(this.Count);

            for (int i = 0; i < this.Count; i++)
            {
                v.AddComponent(this[i].PowerScalar(scalar));
            }
            return(v);
        }
Beispiel #5
0
        /// <summary>
        /// Subtract scalar from the vector components.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private QsVector SubtractScalar(QsScalar s)
        {
            QsVector v = new QsVector(this.Count);

            for (int i = 0; i < this.Count; i++)
            {
                v.AddComponent(this[i] - s);
            }

            return(v);
        }
Beispiel #6
0
        /// <summary>
        /// Differentiate every component in vectpr with the scalar.
        /// </summary>
        /// <param name="scalar"></param>
        /// <returns></returns>
        public QsVector DifferentiateScalar(QsScalar scalar)
        {
            QsVector v = new QsVector(this.Count);

            for (int i = 0; i < this.Count; i++)
            {
                v.AddComponent((QsScalar)this[i].DifferentiateScalar(scalar));
            }

            return(v);
        }
Beispiel #7
0
        private QsValue ModuloScalar(QsScalar scalar)
        {
            QsVector v = new QsVector(this.Count);

            for (int i = 0; i < this.Count; i++)
            {
                v.AddComponent(this[i] % scalar);
            }

            return(v);
        }
Beispiel #8
0
        public QsVector DivideScalar(QsScalar scalar)
        {
            QsVector v = new QsVector(this.Count);

            for (int i = 0; i < this.Count; i++)
            {
                v.AddComponent(this[i] / scalar);
            }

            return(v);
        }
Beispiel #9
0
        private QsVector MultiplyScalar(QsScalar s)
        {
            QsVector v = new QsVector(this.Count);

            for (int i = 0; i < this.Count; i++)
            {
                v.AddComponent(this[i] * s);
            }

            return(v);
        }
Beispiel #10
0
        /// <summary>
        /// Parse Vector text.
        /// </summary>
        /// <param name="vector"></param>
        /// <returns>QsVector on the form of QsValue.</returns>
        public static QsValue ParseVector(string vector)
        {
            string[] qs = vector.Split(',');
            QsVector v  = new QsVector(qs.Length);

            foreach (string q in qs)
            {
                v.AddComponent(new QsScalar {
                    NumericalQuantity = q.ToQuantity()
                });
            }
            return(v);
        }
Beispiel #11
0
        /*
         * Scalar prodcut is commutative that is a{}.b{} = b{}.a{}
         * a{}.b{} = a1b1+a2b2+anbn
         */


        /// <summary>
        /// Dot product of two vectors
        /// </summary>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        /// <returns></returns>
        public QsScalar ScalarProduct(QsVector vector)
        {
            if (this.Count != vector.Count)
            {
                throw new QsException("Vectors are not equal");
            }

            QsScalar Total = this[0] * vector[0];

            for (int i = 1; i < this.Count; i++)
            {
                Total = Total + (this[i] * vector[i]);
            }

            return(Total);
        }
Beispiel #12
0
        /// <summary>
        /// Multiply vector component by component.
        /// </summary>
        /// <param name="vector"></param>
        /// <returns></returns>
        public QsVector MultiplyVector(QsVector vector)
        {
            if (this.Count != vector.Count)
            {
                throw new QsException("Vectors are not equal");
            }

            QsVector v = QsVector.CopyVector(this);

            for (int ix = 0; ix < this.Count; ix++)
            {
                v[ix] = v[ix].MultiplyScalar(vector[ix]);
            }

            return(v);
        }
Beispiel #13
0
        /// <summary>
        /// Subtract two vectors
        /// </summary>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        /// <returns></returns>
        public QsVector SubtractVector(QsVector vector)
        {
            if (this.Count != vector.Count)
            {
                throw new QsException("Vectors are not equal");
            }

            QsVector v = QsVector.CopyVector(this);

            for (int ix = 0; ix < this.Count; ix++)
            {
                v[ix] = v[ix] - vector[ix];
            }

            return(v);
        }
Beispiel #14
0
        private QsValue ModuloVector(QsVector vector)
        {
            if (this.Count != vector.Count)
            {
                throw new QsException("Vectors are not equal");
            }


            QsVector v = QsVector.CopyVector(this);

            for (int ix = 0; ix < this.Count; ix++)
            {
                v[ix] = v[ix] % vector[ix];
            }

            return(v);
        }
Beispiel #15
0
        /// <summary>
        /// Form a matrix from two vectors
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public override QsValue TensorProductOperation(QsValue vl)
        {
            QsValue value;

            if (vl is QsReference)
            {
                value = ((QsReference)vl).ContentValue;
            }
            else
            {
                value = vl;
            }


            if (value is QsScalar)
            {
                throw new NotSupportedException();
            }
            else if (value is QsVector)
            {
                var vec = (QsVector)value;

                if (vec.Count != this.Count)
                {
                    throw new QsException("Not equal vector components");
                }

                List <QsVector> vcs = new List <QsVector>();
                foreach (var c in this)
                {
                    QsVector v = (QsVector)(c * vec);
                    vcs.Add(v);
                }

                return(new QsMatrix(vcs.ToArray()));
            }
            else
            {
                throw new NotSupportedException();
            }
        }
Beispiel #16
0
        public override QsValue RightShiftOperation(QsValue vl)
        {
            QsValue times;

            if (vl is QsReference)
            {
                times = ((QsReference)vl).ContentValue;
            }
            else
            {
                times = vl;
            }


            int itimes = Qs.IntegerFromQsValue((QsScalar)times);

            if (itimes > this.Count)
            {
                itimes = itimes % this.Count;
            }

            // 1 2 3 4 5 >> 2  == 4 5 1 2 3

            QsVector vec = new QsVector(this.Count);

            for (int i = this.Count - itimes; i < this.Count; i++)
            {
                vec.AddComponent(this[i]);
            }

            for (int i = 0; i < (this.Count - itimes); i++)
            {
                vec.AddComponent(this[i]);
            }


            return(vec);
        }
Beispiel #17
0
        /// <summary>
        /// Cross product for 3 components vector.
        /// </summary>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        /// <returns></returns>
        public QsVector VectorProduct(QsVector v2)
        {
            if (this.Count != v2.Count)
            {
                throw new QsException("Vectors are not equal");
            }
            if (this.Count != 3)
            {
                throw new QsException("Cross product only happens with 3 component vector");
            }

            // cross product as determinant of matrix.

            QsMatrix mat = new QsMatrix(
                new QsVector(QsScalar.One, QsScalar.One, QsScalar.One)
                , this
                , v2);

            return(mat.Determinant());

            // problem now: what if we have more than 3 elements in the vector.
            // there is no cross product for more than 3 elements for vectors.
        }