Exemplo n.º 1
0
 /// <summary>
 /// Returns the code for any negation, reversion, conjugation or grade involution function of general multivectors.
 /// The code is composed of calls to functions generated by <c>WriteCASNparts()</c>.
 /// 
 /// The returned code is only the body. The function declaration is not included.
 /// </summary>
 /// <param name="S">Specification of algebra (used for output language).</param>
 /// <param name="cgd">Currently not used.</param>
 /// <param name="FT">Floating point type.</param>
 /// <param name="T">What function to generate</param>
 /// <param name="FAI">Info about function arguments</param>
 /// <param name="resultName">Name of variable where the result goes (in the generated code).</param>
 /// <returns>code for the requested product type.</returns>
 public static string GetUnaryToggleSignCode(Specification S, G25.CG.Shared.CGdata cgd, G25.FloatType FT, 
     UnaryToggleSignType T,
     G25.CG.Shared.FuncArgInfo[] FAI, string resultName)
 {
     if (S.OutputCppOrC())
         return GetUnaryToggleSignCodeCppOrC(S, cgd, FT, T, FAI, resultName);
     else return GetUnaryToggleSignCodeCSharpOrJava(S, cgd, FT, T, FAI, resultName);
 }
Exemplo n.º 2
0
        private static string GetUnaryToggleSignCodeCSharpOrJava(Specification S, G25.CG.Shared.CGdata cgd, G25.FloatType FT,
            UnaryToggleSignType T,
            G25.CG.Shared.FuncArgInfo[] FAI, string resultName)
        {
            G25.GMV gmv = S.m_GMV;

            StringBuilder SB = new StringBuilder();

            // get number of groups:
            int nbGroups = gmv.NbGroups;

            bool resultIsScalar = false , initResultToZero = false;
            SB.Append(GPparts.GetExpandCode(S, cgd, FT, FAI, resultIsScalar, initResultToZero));

            // for each group
            // test if present, then code / negate, etc

            for (int g = 0; g < nbGroups; g++)
            {
                SB.AppendLine("");

                // do we need to negate this group?
                bool neg = NeedToNegate(gmv, g, T);

                string funcName = (neg) ? GetNegPartFunctionName(S, FT, g) : GetCopyPartFunctionName(S, FT, g);

                SB.AppendLine("if (ac[" + g + "] != null) {");
                SB.AppendLine("\tcc[" + g + "] = new " + FT.type + "[" + gmv.Group(g).Length + "];");
                SB.AppendLine("\t" + funcName + "(ac[" + g + "], cc[" + g + "]);");
                SB.AppendLine("}");
            }

            SB.AppendLine("return new " + FT.GetMangledName(S, gmv.Name) + "(cc);");

            return SB.ToString();
        }
Exemplo n.º 3
0
 /// <returns>Whether to negate group 'groupIdx' for sign toggle operation 'T'.</returns>
 private static bool NeedToNegate(G25.GMV gmv, int groupIdx, UnaryToggleSignType T)
 {
     switch (T)
     {
         case UnaryToggleSignType.NEGATE:
             return true;
         case UnaryToggleSignType.REVERSE:
             return (gmv.Group(groupIdx)[0].Reverse().scale / gmv.Group(groupIdx)[0].scale) < 0;
         case UnaryToggleSignType.GRADE_INVOLUTION:
             return (gmv.Group(groupIdx)[0].GradeInvolution().scale / gmv.Group(groupIdx)[0].scale) < 0;
         case UnaryToggleSignType.CLIFFORD_CONJUGATE:
             return (gmv.Group(groupIdx)[0].CliffordConjugate().scale / gmv.Group(groupIdx)[0].scale) < 0;
         default:
             return false;
     }
 }
Exemplo n.º 4
0
        private static string GetUnaryToggleSignCodeCppOrC(Specification S, G25.CG.Shared.CGdata cgd, G25.FloatType FT, 
            UnaryToggleSignType T,
            G25.CG.Shared.FuncArgInfo[] FAI, string resultName)
        {
            G25.GMV gmv = S.m_GMV;

            StringBuilder SB=  new StringBuilder();

            string agu = (S.OutputC()) ? FAI[0].Name + "->gu" : FAI[0].Name + ".gu()";
            string ac = (S.OutputC()) ? FAI[0].Name + "->c" : FAI[0].Name + ".getC()";
            string resultCoordPtr = (S.OutputC()) ? resultName + "->c" : "c";

            SB.AppendLine("int idx = 0;");

            if (S.OutputC())
            {
                SB.AppendLine(resultName + "->gu = " + agu + ";");
            }
            else
            {
                SB.AppendLine("int gu = " + agu + ";");
                SB.AppendLine(FT.type + " c[" + (1 << S.m_dimension) + "];");
            }

            // get number of groups:
            int nbGroups = gmv.NbGroups;

            // for each group
            // test if present, then code / negate, etc

            for (int g = 0; g < nbGroups; g++)
            {
                SB.AppendLine("");

                // do we need to negate this group?
                bool neg = NeedToNegate(gmv, g, T);

                string funcName = (neg) ? GetNegPartFunctionName(S, FT, g) : GetCopyPartFunctionName(S, FT, g);

                SB.AppendLine("if (" + agu + " & " + (1 << g) + ") {");
                SB.AppendLine("\t" + funcName + "(" + ac + " + idx, " + resultCoordPtr + " + idx);");

                if (g < (nbGroups - 1)) SB.AppendLine("\tidx += " + gmv.Group(g).Length + ";");

                SB.AppendLine("}");
            }

            // return result
            if (S.m_outputLanguage != OUTPUT_LANGUAGE.C)
            {
                SB.AppendLine("return " + FT.GetMangledName(S, gmv.Name) + "(gu, c);");
            }

            return SB.ToString();
        }