Exemplo n.º 1
0
        public GaPoTNumBiversor[] GetPartsImpedance(GaPoTNumVector current, params int[] partLengthsArray)
        {
            var mvU = GetParts(partLengthsArray);
            var mvI = current.GetParts(partLengthsArray).Inverse();

            return(mvU.Gp(mvI));
        }
        public static GaPoTNumRotorsSequence CreateFromOrthonormalFrames(GaPoTNumFrame sourceFrame, GaPoTNumFrame targetFrame)
        {
            Debug.Assert(targetFrame.Count == sourceFrame.Count);
            Debug.Assert(sourceFrame.IsOrthonormal() && targetFrame.IsOrthonormal());
            Debug.Assert(sourceFrame.HasSameHandedness(targetFrame));

            var rotorsSequence = new GaPoTNumRotorsSequence();

            var sourceFrameVectors = new GaPoTNumVector[sourceFrame.Count];

            for (var i = 0; i < sourceFrame.Count; i++)
            {
                sourceFrameVectors[i] = sourceFrame[i];
            }

            for (var i = 0; i < sourceFrame.Count - 1; i++)
            {
                var sourceVector = sourceFrameVectors[i];
                var targetVector = targetFrame[i];

                var rotor =
                    sourceVector.GetRotorToVector(targetVector);

                rotorsSequence.AppendRotor(rotor);

                for (var j = i + 1; j < sourceFrame.Count; j++)
                {
                    sourceFrameVectors[j] = sourceFrameVectors[j].ApplyRotor(rotor);
                }
            }

            return(rotorsSequence);
        }
Exemplo n.º 3
0
        public static GaPoTNumMultivector CreateSimpleRotor(GaPoTNumVector sourceVector, GaPoTNumVector targetVector)
        {
            var invNorm1 = 1.0d / sourceVector.Norm();
            var invNorm2 = 1.0d / targetVector.Norm();
            var cosAngle = sourceVector.DotProduct(targetVector) * invNorm1 * invNorm2;

            if (cosAngle == 1.0d)
            {
                return(new GaPoTNumMultivector().SetTerm(0, 1.0d));
            }

            //TODO: Handle the case for cosAngle == -1

            var cosHalfAngle  = Math.Sqrt(0.5d * (1.0d + cosAngle));
            var sinHalfAngle  = Math.Sqrt(0.5d * (1.0d - cosAngle));
            var rotationBlade = sourceVector.Op(targetVector);

            var rotationBladeScalar =
                sinHalfAngle /
                Math.Sqrt(Math.Abs(rotationBlade.Gp(rotationBlade).GetTermValue(0)));

            var rotor = cosHalfAngle - rotationBladeScalar * rotationBlade;

            //var rotationAngle = Math.Acos(DotProduct(v2) * invNorm1 * invNorm2) / 2;
            //var unitBlade = rotationBlade.ScaleBy(rotationBladeInvNorm);
            //var unitBladeNorm = unitBlade.Gp(unitBlade).TermsToText();
            //var rotor= Math.Cos(rotationAngle) - (rotationBladeInvNorm * Math.Sin(rotationAngle)) * rotationBlade;

            //Normalize rotor
            //var invRotorNorm = 1.0d / Math.Sqrt(rotor.Gp(rotor.Reverse()).GetTermValue(0));

            //rotor.IsSimpleRotor();

            return(rotor);
        }
Exemplo n.º 4
0
        public static GaPoTNumVector operator -(GaPoTNumVector v)
        {
            var result = new GaPoTNumVector();

            foreach (var term in v._termsDictionary.Values)
            {
                result.AddTerm(term.TermId, -term.Value);
            }

            return(result);
        }
Exemplo n.º 5
0
        public double DotProduct(GaPoTNumVector v)
        {
            var result = 0.0d;

            foreach (var term1 in _termsDictionary.Values)
            {
                if (v._termsDictionary.TryGetValue(term1.TermId, out var term2))
                {
                    result += term1.Value * term2.Value;
                }
            }

            return(result);
        }
Exemplo n.º 6
0
        public GaPoTNumVector GetProjectionOnFrame(GaPoTNumFrame frame)
        {
            var vector = new GaPoTNumVector();

            foreach (var term in _termsDictionary.Values)
            {
                var i = term.TermId - 1;
                var v = term.Value;

                vector += v * frame[i];
            }

            return(vector);
        }
Exemplo n.º 7
0
        public static GaPoTNumVector operator /(GaPoTNumVector v, double s)
        {
            s = 1.0d / s;

            var result = new GaPoTNumVector();

            foreach (var term in v._termsDictionary.Values)
            {
                result.AddTerm(
                    term.TermId,
                    term.Value * s
                    );
            }

            return(result);
        }
Exemplo n.º 8
0
        public static GaPoTNumVector[] Subtract(this GaPoTNumVector[] vectorsList1, GaPoTNumVector[] vectorsList2)
        {
            if (vectorsList1.Length != vectorsList2.Length)
            {
                throw new InvalidOperationException();
            }

            var results = new GaPoTNumVector[vectorsList1.Length];

            for (var i = 0; i < vectorsList1.Length; i++)
            {
                results[i] = vectorsList1[i].Subtract(vectorsList2[i]);
            }

            return(results);
        }
        public static GaPoTNumRotorsSequence CreateFromFrames(int baseSpaceDimensions, GaPoTNumFrame sourceFrame, GaPoTNumFrame targetFrame)
        {
            Debug.Assert(targetFrame.Count == sourceFrame.Count);
            //Debug.Assert(IsOrthonormal() && targetFrame.IsOrthonormal());
            Debug.Assert(sourceFrame.HasSameHandedness(targetFrame));

            var rotorsSequence = new GaPoTNumRotorsSequence();

            var pseudoScalar =
                GaPoTNumMultivector
                .CreateZero()
                .SetTerm((1 << baseSpaceDimensions) - 1, 1.0d);

            var sourceFrameVectors = new GaPoTNumVector[sourceFrame.Count];
            var targetFrameVectors = new GaPoTNumVector[targetFrame.Count];

            for (var i = 0; i < sourceFrame.Count; i++)
            {
                sourceFrameVectors[i] = sourceFrame[i];
                targetFrameVectors[i] = targetFrame[i];
            }

            for (var i = 0; i < sourceFrame.Count - 1; i++)
            {
                var sourceVector = sourceFrameVectors[i];
                var targetVector = targetFrameVectors[i];

                var rotor =
                    sourceVector.GetRotorToVector(targetVector);

                rotorsSequence.AppendRotor(rotor);

                pseudoScalar = targetVector.ToMultivector().Lcp(pseudoScalar.Inverse());

                for (var j = i + 1; j < sourceFrame.Count; j++)
                {
                    sourceFrameVectors[j] =
                        sourceFrameVectors[j].ApplyRotor(rotor).GetProjectionOnBlade(pseudoScalar);

                    targetFrameVectors[j] =
                        targetFrameVectors[j].GetProjectionOnBlade(pseudoScalar);
                }
            }

            return(rotorsSequence);
        }
Exemplo n.º 10
0
        public GaPoTNumVector[] GetOffsetParts(params int[] partLengthsArray)
        {
            var results = new GaPoTNumVector[partLengthsArray.Length];

            var termId1 = 1;

            for (var i = 0; i < partLengthsArray.Length; i++)
            {
                var termId2 = termId1 + partLengthsArray[i] - 1;

                results[i] = GetOffsetPartByTermIDsRange(termId1, termId2);

                termId1 = termId2 + 1;
            }

            return(results);
        }
Exemplo n.º 11
0
        public static GaPoTNumVector operator *(GaPoTNumVector v1, GaPoTNumBiversor bv2)
        {
            var vector = new GaPoTNumVector();

            foreach (var term1 in v1.GetTerms())
            {
                foreach (var term2 in bv2.GetTerms())
                {
                    var scalarValue = term1.Value * term2.Value;

                    if (term2.IsScalar)
                    {
                        vector.AddTerm(
                            term1.TermId,
                            scalarValue
                            );

                        continue;
                    }

                    if (term1.TermId == term2.TermId1)
                    {
                        vector.AddTerm(
                            term2.TermId2,
                            scalarValue
                            );

                        continue;
                    }

                    if (term1.TermId == term2.TermId2)
                    {
                        vector.AddTerm(
                            term2.TermId1,
                            -scalarValue
                            );
                    }
                }
            }

            return(vector);
        }
Exemplo n.º 12
0
        public GaPoTNumVector Inverse()
        {
            var norm2 = Norm2();

            var result = new GaPoTNumVector();

            if (norm2 == 0)
            {
                throw new DivideByZeroException();
            }

            var invNorm2 = 1.0d / norm2;

            foreach (var term in _termsDictionary.Values)
            {
                result.SetTerm(
                    term.TermId,
                    term.Value * invNorm2
                    );
            }

            return(result);
        }
Exemplo n.º 13
0
 public GaPoTNumVector Subtract(GaPoTNumVector v)
 {
     return(this - v);
 }
Exemplo n.º 14
0
 public double GetAngle(GaPoTNumVector v)
 {
     return(Math.Acos(DotProduct(v) / Math.Sqrt(Norm2() * v.Norm2())));
 }
Exemplo n.º 15
0
        private static GaPoTNumVector GaPoTNumParseVector(IronyParsingResults parsingResults, ParseTreeNode rootNode)
        {
            if (rootNode.ToString() != "spVector")
            {
                throw new SyntaxErrorException(parsingResults.ToString());
            }

            var vector = new GaPoTNumVector();

            var vectorNode = rootNode;

            foreach (var vectorElementNode in vectorNode.ChildNodes)
            {
                if (vectorElementNode.ToString() == "spTerm")
                {
                    //Term Form
                    var value = double.Parse(vectorElementNode.ChildNodes[0].FindTokenAndGetText());
                    var id    = int.Parse(vectorElementNode.ChildNodes[1].FindTokenAndGetText());

                    if (id < 0)
                    {
                        throw new SyntaxErrorException(parsingResults.ToString());
                    }

                    vector.AddTerm(id, value);
                }
                else if (vectorElementNode.ToString() == "spPolarPhasor")
                {
                    //Polar Phasor Form
                    var magnitude = double.Parse(vectorElementNode.ChildNodes[1].FindTokenAndGetText());
                    var phase     = double.Parse(vectorElementNode.ChildNodes[2].FindTokenAndGetText());
                    var id1       = int.Parse(vectorElementNode.ChildNodes[3].FindTokenAndGetText());
                    var id2       = int.Parse(vectorElementNode.ChildNodes[4].FindTokenAndGetText());

                    if (id1 < 0 || id2 != id1 + 1)
                    {
                        throw new SyntaxErrorException(parsingResults.ToString());
                    }

                    //Convert phase from degrees to radians
                    phase = phase.DegreesToRadians();

                    vector.AddPolarPhasor(id1, magnitude, phase);
                }
                else if (vectorElementNode.ToString() == "spRectPhasor")
                {
                    //Rectangular Phasor Form
                    var xValue = double.Parse(vectorElementNode.ChildNodes[1].FindTokenAndGetText());
                    var yValue = double.Parse(vectorElementNode.ChildNodes[2].FindTokenAndGetText());
                    var id1    = int.Parse(vectorElementNode.ChildNodes[3].FindTokenAndGetText());
                    var id2    = int.Parse(vectorElementNode.ChildNodes[4].FindTokenAndGetText());

                    if (id1 < 0 || id2 != id1 + 1)
                    {
                        throw new SyntaxErrorException(parsingResults.ToString());
                    }

                    vector.AddRectPhasor(id1, xValue, yValue);
                }
                else
                {
                    throw new SyntaxErrorException(parsingResults.ToString());
                }
            }

            return(vector);
        }
Exemplo n.º 16
0
 public GaPoTNumVector Add(GaPoTNumVector v)
 {
     return(this + v);
 }
Exemplo n.º 17
0
 public GaPoTNumMultivector Op(GaPoTNumVector v)
 {
     return(Op(v.ToMultivector()));
 }
Exemplo n.º 18
0
 public GaPoTNumVector Gp(GaPoTNumVector v)
 {
     return(this * v);
 }
Exemplo n.º 19
0
 public GaPoTNumMultivector GetRotorToVector(GaPoTNumVector v2)
 {
     return(GaPoTNumMultivector.CreateSimpleRotor(this, v2));
 }