本文整理汇总了C#中Specification.OutputCSharpOrJava方法的典型用法代码示例。如果您正苦于以下问题:C# Specification.OutputCSharpOrJava方法的具体用法?C# Specification.OutputCSharpOrJava怎么用?C# Specification.OutputCSharpOrJava使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Specification
的用法示例。
在下文中一共展示了Specification.OutputCSharpOrJava方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteSetVectorImages
//.........这里部分代码省略.........
List<G25.CG.Shared.Instruction>[] bladeI = new List<G25.CG.Shared.Instruction>[1 << S.m_dimension];
{
bool mustCast = false;
int nbTabs = 1;
string dstName = (S.OutputC()) ? G25.fgs.RETURN_ARG_NAME : SmvUtil.THIS;
bool dstPtr = S.OutputCppOrC();
bool declareDst = false;
for (int g = 1; g < gom.Domain.Length; g++)
{
for (int d = 0; d < gom.DomainForGrade(g).Length; d++)
{
G25.SMVOM smvOM = gom.DomainSmvForGrade(g)[d];
RefGA.BasisBlade domainBlade = gom.DomainForGrade(g)[d];
if (g > 1)
{
bladeI[domainBlade.bitmap] = new List<G25.CG.Shared.Instruction>();
string funcCallCode = funcNames[g] + "_" + d + "(";
if (S.OutputC()) funcCallCode += G25.fgs.RETURN_ARG_NAME;
funcCallCode += ");";
mainI.Add(new G25.CG.Shared.VerbatimCodeInstruction(nbTabs, funcCallCode));
}
// follow the plan
RefGA.Multivector value = new RefGA.Multivector(signs[g][d]);
uint[] P = plan[g][d];
for (int p = 0; p < P.Length; p++)
value = RefGA.Multivector.op(value, symbolicBBvalues[P[p]]);
// add instructions
List<G25.CG.Shared.Instruction> I = (g == 1) ? mainI : bladeI[domainBlade.bitmap];
I.Add(new G25.CG.Shared.CommentInstruction(nbTabs, "Set image of " + domainBlade.ToString(S.m_basisVectorNames)));
I.Add(new G25.CG.Shared.AssignInstruction(nbTabs, smvOM, FT, mustCast, value, dstName, dstPtr, declareDst));
// store symbolic value
symbolicBBvalues[domainBlade.bitmap] = G25.CG.Shared.Symbolic.SMVtoSymbolicMultivector(S, smvOM, dstName, dstPtr);
}
}
}
// output grade > 1 functions
if (cgd.generateOmInitCode(FT.type))
{
for (int g = 2; g < gom.Domain.Length; g++)
{
for (int d = 0; d < gom.DomainForGrade(g).Length; d++)
{
RefGA.BasisBlade domainBlade = gom.DomainForGrade(g)[d];
string funcName = funcNames[g] + "_" + d;
G25.fgs F = new G25.fgs(funcName, funcName, "", new string[0], new string[0], new string[] { FT.type }, null, null, null); // null, null = metricName, comment, options
//F.InitArgumentPtrFromTypeNames(S);
bool computeMultivectorValue = false;
G25.CG.Shared.FuncArgInfo returnArgument = null;
if (S.OutputC())
returnArgument = new G25.CG.Shared.FuncArgInfo(S, F, -1, FT, gom.Name, computeMultivectorValue);
int nbArgs = 0;
G25.CG.Shared.FuncArgInfo[] FAI = G25.CG.Shared.FuncArgInfo.GetAllFuncArgInfo(S, F, nbArgs, FT, S.m_GMV.Name, computeMultivectorValue);
Comment comment;
comment = new Comment("Sets grade " + g + " part of outermorphism matrix based on lower grade parts.");
bool inline = false; // do not inline this potentially huge function
bool staticFunc = false;
bool writeDecl = S.OutputC();
G25.CG.Shared.Functions.WriteFunction(S, cgd, F, inline, staticFunc, "void", funcName, returnArgument, FAI, bladeI[domainBlade.bitmap], comment, writeDecl);
}
}
}
{ // output grade 1 function
G25.fgs F = new G25.fgs(funcNames[1], funcNames[1], "", argTypes, argNames, new string[] { FT.type }, null, null, null); // null, null = metricName, comment, options
F.InitArgumentPtrFromTypeNames(S);
if (matrixMode)
{
F.m_argumentPtr[0] = S.OutputCppOrC();
F.m_argumentArr[0] = S.OutputCSharpOrJava();
}
bool computeMultivectorValue = false;
G25.CG.Shared.FuncArgInfo returnArgument = null;
if (S.OutputC())
returnArgument = new G25.CG.Shared.FuncArgInfo(S, F, -1, FT, gom.Name, computeMultivectorValue);
G25.CG.Shared.FuncArgInfo[] FAI = G25.CG.Shared.FuncArgInfo.GetAllFuncArgInfo(S, F, NB_ARGS, FT, S.m_GMV.Name, computeMultivectorValue);
Comment comment;
if (!matrixMode) comment = new Comment("Sets " + typeName + " from images of the domain vectors.");
else comment = new Comment("Sets " + typeName + " from a " + (transpose ? "transposed " : "") + "matrix");
bool inline = false; // do not inline this potentially huge function
bool staticFunc = false;
bool writeDecl = S.OutputC();
G25.CG.Shared.Functions.WriteFunction(S, cgd, F, inline, staticFunc, "void", funcNames[1], returnArgument, FAI, mainI, comment, writeDecl);
}
}
示例2: WriteGomParts
/// <summary>
/// Generates functions which compute parts of the application of a general outermorphism to a general multivector.
///
/// This function should be called early on in the code generation process, at least
/// before any of the <c>???()</c> functions is called.
/// </summary>
/// <param name="S">Specification (used for output language, GMV).</param>
/// <param name="cgd">Where the result goes.</param>
public static void WriteGomParts(Specification S, CGdata cgd)
{
if (S.m_GOM == null) return; // nothing to do if GOM not defiend
int nbBaseTabs = (S.OutputCSharpOrJava()) ? 1 : 0;
int nbCodeTabs = nbBaseTabs + 1;
G25.GMV gmv = S.m_GMV;
G25.GOM gom = S.m_GOM;
string nameGOM = "O";
string nameSrcGMV = "A";
string nameDstGMV = "C";
// get symbolic multivector value
RefGA.Multivector[] M1 = null;
{
bool ptr = (S.OutputC());
int allGroups = -1;
M1 = G25.CG.Shared.Symbolic.GMVtoSymbolicMultivector(S, gmv, nameSrcGMV, ptr, allGroups);
}
foreach (G25.FloatType FT in S.m_floatTypes)
{
// map from code fragment to name of function
Dictionary<string, string> generatedCode = new Dictionary<string, string>();
// loop over all groups of the GMV, multiply with GOM, and assign the result
for (int srcGroup = 0; srcGroup < gmv.NbGroups; srcGroup++)
{
RefGA.Multivector inputValue = M1[srcGroup];
if (inputValue.IsScalar()) continue;
// Replace each basis blade in 'inputValue' with its value under the outermorphism.
RefGA.Multivector returnValue = RefGA.Multivector.ZERO; // returnValue = gom * gmv[srcGroup]
for (int i = 0; i < inputValue.BasisBlades.Length; i++)
{
// get input blade and domain for that grade
RefGA.BasisBlade inputBlade = inputValue.BasisBlades[i];
RefGA.BasisBlade[] domainBlades = gom.DomainForGrade(inputBlade.Grade());
for (int c = 0; c < domainBlades.Length; c++)
{
// if a match is found in the domain, add range vector to m_returnValue
if (domainBlades[c].bitmap == inputBlade.bitmap)
{
bool ptr = (S.OutputC());
RefGA.Multivector omColumnValue = G25.CG.Shared.Symbolic.SMVtoSymbolicMultivector(S, gom.DomainSmvForGrade(inputBlade.Grade())[c], nameGOM, ptr);
RefGA.Multivector inputBladeScalarMultiplier = new RefGA.Multivector(new RefGA.BasisBlade(inputBlade, 0));
RefGA.Multivector domainBladeScalarMultiplier = new RefGA.Multivector(new RefGA.BasisBlade(domainBlades[c], 0));
returnValue = RefGA.Multivector.Add(returnValue,
RefGA.Multivector.gp(
RefGA.Multivector.gp(omColumnValue, inputBladeScalarMultiplier),
domainBladeScalarMultiplier));
break; // no need to search the other domainBlades too
}
}
} // end of 'compute return value'
// assign returnValue to various groups of the gmv
for (int dstGroup = 0; dstGroup < gmv.NbGroups; dstGroup++)
{
bool mustCast = false;
bool writeZeros = false; // no need to generate "+= 0.0;"
int dstBaseIdx = 0;
string code = G25.CG.Shared.CodeUtil.GenerateGMVassignmentCode(S, FT, mustCast, gmv, nameDstGMV, dstGroup, dstBaseIdx, returnValue, nbCodeTabs, writeZeros);
string funcName = GetGomPartFunctionName(S, FT, srcGroup, dstGroup);
cgd.m_gmvGomPartFuncNames[new Tuple<string, string>(FT.type, funcName)] = (code.Length > 0);
if (code.Length == 0) continue;
if (!S.m_GMV.IsGroupedByGrade(S.m_dimension))
code = code.Replace("=", "+=");
// check if code was already generated, and, if so, reuse it
if (generatedCode.ContainsKey(code))
{
// ready generated: call that function
code = "\t" + generatedCode[code] + "(" + nameGOM + ", " + nameSrcGMV + ", " + nameDstGMV + ");\n";
}
else
{
// not generated yet: remember code -> function
generatedCode[code] = funcName;
}
// write comment
string comment = "Computes the partial application of a general outermorphism to a general multivector";
string OM_PTR = "";
if (S.OutputC()) OM_PTR = "*";
else if (S.OutputCpp()) OM_PTR = "&";
//.........这里部分代码省略.........
示例3: WriteOMtoOMcopy
public static void WriteOMtoOMcopy(Specification S, G25.CG.Shared.CGdata cgd, G25.FloatType FT, OM srcOm, OM dstOm)
{
StringBuilder declSB = cgd.m_declSB;
StringBuilder defSB = (S.m_inlineSet) ? cgd.m_inlineDefSB : cgd.m_defSB;
string srcTypeName = FT.GetMangledName(S, srcOm.Name);
string dstTypeName = FT.GetMangledName(S, dstOm.Name);
bool writeDecl = S.OutputC();
string funcName = GetFunctionName(S, (S.OutputC() ? "" : dstTypeName), "set", srcTypeName + "_to_" + dstTypeName);
// do we inline this func?
string inlineStr = G25.CG.Shared.Util.GetInlineString(S, S.m_inlineSet, " ");
string dstArgStr = (S.OutputC()) ? (dstTypeName + " *dst, ") : "";
string refStr = (S.OutputC()) ? "*" : ((S.OutputCpp()) ? "&" : "");
string CONST = (S.OutputCppOrC()) ? "const " : "";
string funcDecl = inlineStr + "void " + funcName + "(" + dstArgStr + CONST + srcTypeName + " " + refStr + "src)";
// write comment, function declaration
Comment comment = new Comment("Copies a " + srcTypeName + " to a " + dstTypeName + "\n" +
"Warning 1: coordinates which cannot be represented are silenty lost.\n" +
"Warning 2: coordinates which are not present in 'src' are set to zero in 'dst'.\n");
if (writeDecl)
{
comment.Write(declSB, S, 0);
declSB.Append(funcDecl);
declSB.AppendLine(";");
}
if (S.OutputCSharpOrJava())
comment.Write(defSB, S, 0);
defSB.Append(funcDecl);
{
defSB.AppendLine(" {");
Dictionary<Tuple<int, int, int>, Tuple<int, int, double>> D = dstOm.getMapping(srcOm);
StringBuilder copySB = new StringBuilder();
List<string> setToZero = new List<string>();
string matrixStr = (S.OutputC()) ? "m" : "m_m";
string dstMatrixStr = (S.OutputC()) ? "dst->" + matrixStr : matrixStr;
string ptrStr = (S.OutputC()) ? "->" : ".";
// For all grades of som, for all columns, for all rows, check D, get entry, set; otherwise set to null
// Do not use foreach() on D because we want to fill in coordinates in their proper order.
for (int gradeIdx = 1; gradeIdx < dstOm.Domain.Length; gradeIdx++)
{
for (int somRangeIdx = 0; somRangeIdx < dstOm.Range[gradeIdx].Length; somRangeIdx++)
{
for (int somDomainIdx = 0; somDomainIdx < dstOm.Domain[gradeIdx].Length; somDomainIdx++)
{
Tuple<int, int, int> key = new Tuple<int, int, int>(gradeIdx, somDomainIdx, somRangeIdx);
int somMatrixIdx = dstOm.getCoordinateIndex(gradeIdx, somDomainIdx, somRangeIdx);
string dstString = dstMatrixStr + gradeIdx + "[" + somMatrixIdx + "] = ";
if (D.ContainsKey(key))
{
Tuple<int, int, double> value = D[key];
int gomMatrixIdx = srcOm.getCoordinateIndex(gradeIdx, value.Value1, value.Value2);
double multiplier = value.Value3;
string multiplierString = (multiplier == 1.0) ? "" : (FT.DoubleToString(S, multiplier) + " * ");
copySB.AppendLine("\t" + dstString + multiplierString + " src" + ptrStr + matrixStr + gradeIdx + "[" + gomMatrixIdx + "];");
}
else
{
setToZero.Add(dstString);
}
}
}
}
// append copy statements
defSB.Append(copySB);
// append statements to set coordinates to zero
if (setToZero.Count > 0)
{
int cnt = 0;
defSB.Append("\t");
foreach (string str in setToZero)
{
defSB.Append(str);
cnt++;
if (cnt > 8)
{
cnt = 0;
defSB.AppendLine("");
defSB.Append("\t\t");
}
}
defSB.AppendLine(FT.DoubleToString(S, 0.0) + ";");
}
defSB.AppendLine("}");
}
}
示例4: GetIncrementCode
/// <summary>
/// Returns the code for <c>gmv + scalar</c>
///
/// The code is NOT composed of calls to functions generated by <c>WriteCANSparts()</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="FAI">Info about function arguments</param>
/// <param name="resultName">Name of variable where the result goes (in the generated code).</param>
/// <param name="increment">Whether write increment or decrement function.</param>
/// <returns>code for the requested function.</returns>
public static string GetIncrementCode(Specification S, G25.CG.Shared.CGdata cgd, G25.FloatType FT,
G25.CG.Shared.FuncArgInfo[] FAI, string resultName, bool increment)
{
G25.GMV gmv = S.m_GMV;
StringBuilder SB = new StringBuilder();
if (S.OutputC())
{
SB.AppendLine(FT.type + " val" + " = " +
FAI[0].MangledTypeName + "_" + gmv.Group(0)[0].ToLangString(S.m_basisVectorNames) + "(" + FAI[0].Name + ")" +
((increment) ? " + " : " - ") + FT.DoubleToString(S, 1.0) + ";");
SB.AppendLine(FAI[0].MangledTypeName + "_copy(" + resultName + ", " + FAI[0].Name + ");");
SB.AppendLine(FAI[0].MangledTypeName + "_set_" + gmv.Group(0)[0].ToLangString(S.m_basisVectorNames) + "(" + resultName + ", val);");
}
else
{
string convertMvIfCode = (S.OutputCSharpOrJava()) ? (".to_" + FAI[0].MangledTypeName + "()") : "";
if (S.OutputCpp())
SB.AppendLine(FAI[0].MangledTypeName + " " + resultName + "(" + FAI[0].Name + ");");
else SB.AppendLine(FAI[0].MangledTypeName + " " + resultName + " = new " + FAI[0].MangledTypeName + "(" + FAI[0].Name + convertMvIfCode + ");");
SB.AppendLine(FT.type + " val" + " = " + resultName + ".get_" + gmv.Group(0)[0].ToLangString(S.m_basisVectorNames) + "()" +
((increment) ? " + " : " - ") + FT.DoubleToString(S, 1.0) + ";");
SB.AppendLine(resultName + ".set_" + gmv.Group(0)[0].ToLangString(S.m_basisVectorNames) + "(val);");
SB.AppendLine("return " + resultName + ";");
}
return SB.ToString();
}
示例5: WriteCANSparts
/// <summary>
/// Writes functions for the copying, adding, subtracting, negating, scaling
/// inverse scaling and Hadamard product of general multivectors, on a group by group basis.
///
/// Internally the function loops over all float types, and over all operations (8 in total)
/// to generate all code.
///
/// This function should be called early on in the code generation process, at least
/// before any of the <c>Get....Code()</c> functions is called.
/// </summary>
/// <param name="S">Specification (used for floating points types, output language, GMV).</param>
/// <param name="cgd">Output goes here.</param>
public static void WriteCANSparts(Specification S, CGdata cgd)
{
G25.GMV gmv = S.m_GMV;
string srcName1 = "A";
string srcName2 = "B";
string dstName = "C";
string epsilonName = "eps";
string scaleName = "s";
bool ptr = true;
int allGroups = -1;
bool mustCast = false;
int nbBaseTabs = (S.OutputCSharpOrJava()) ? 1 : 0;
int nbCodeTabs = nbBaseTabs + 1;
bool writeZeros = false;
// get two symbolic multivectors (with different symbolic names):
RefGA.Multivector[] M1 = null, M2 = null;
if (S.m_gmvCodeGeneration == GMV_CODE.EXPAND)
{ // M1 and M2 and only required for full code expansion
M1 = G25.CG.Shared.Symbolic.GMVtoSymbolicMultivector(S, gmv, srcName1, ptr, allGroups);
M2 = G25.CG.Shared.Symbolic.GMVtoSymbolicMultivector(S, gmv, srcName2, ptr, allGroups);
}
RefGA.Multivector scaleM = new RefGA.Multivector(scaleName);
foreach (G25.FloatType FT in S.m_floatTypes)
{
// map from code fragment to name of function
Dictionary<string, string> generatedCode = new Dictionary<string, string>();
for (int g1 = 0; g1 < gmv.NbGroups; g1++)
{
int g2 = g1;
const int COPY = 0;
const int COPY_MUL = 1;
const int COPY_DIV = 2;
const int ADD = 3;
const int SUB = 4;
const int NEG = 5;
const int ADD2 = 6;
const int SUB2 = 7;
const int HP = 8;
const int IHP = 9;
const int EQUALS = 10;
const int ZERO = 11;
const int NB_OPS = 12;
for (int op = 0; op < NB_OPS; op++)
{
// get value of operation, name of function:
RefGA.Multivector value = null;
String funcName = null;
String comment = null;
String code = null; // code depends on code generation mode (expand or run-time)
switch (op)
{
case COPY:
if (S.m_gmvCodeGeneration == GMV_CODE.EXPAND)
value = M1[g1];
else if (S.m_gmvCodeGeneration == GMV_CODE.RUNTIME)
code = GetRuntimeGmvCopyCode(S, cgd, g1, srcName1, dstName);
funcName = GetCopyPartFunctionName(S, FT, g1);
comment = "copies coordinates of group " + g1;
break;
case COPY_MUL:
if (S.m_gmvCodeGeneration == GMV_CODE.EXPAND)
value = RefGA.Multivector.gp(scaleM, M1[g1]);
else if (S.m_gmvCodeGeneration == GMV_CODE.RUNTIME)
code = GetRuntimeGmvCopyMulDivCode(S, cgd, g1, srcName1, dstName, scaleName, "*");
funcName = GetCopyMulPartFunctionName(S, FT, g1);
comment = "copies and multiplies (by " + scaleName + ") coordinates of group " + g1;
break;
case COPY_DIV:
if (S.m_gmvCodeGeneration == GMV_CODE.EXPAND)
value = RefGA.Multivector.gp(M1[g1], scaleM);
else if (S.m_gmvCodeGeneration == GMV_CODE.RUNTIME)
code = GetRuntimeGmvCopyMulDivCode(S, cgd, g1, srcName1, dstName, scaleName, "/");
funcName = GetCopyDivPartFunctionName(S, FT, g1);
comment = "copies and divides (by " + scaleName + ") coordinates of group " + g1;
break;
case ADD:
if (S.m_gmvCodeGeneration == GMV_CODE.EXPAND)
value = M1[g1];
else if (S.m_gmvCodeGeneration == GMV_CODE.RUNTIME)
code = GetRuntimeGmvAddSubNegCode(S, cgd, g1, srcName1, dstName, "+= ");
funcName = GetAddPartFunctionName(S, FT, g1);
comment = "adds coordinates of group " + g1 + " from variable " + srcName1 + " to " + dstName;
//.........这里部分代码省略.........
示例6: GetAnyMetricNormCode
private static string GetAnyMetricNormCode(Specification S, G25.CG.Shared.CGdata cgd, G25.FloatType FT,
G25.Metric M, bool squared,
G25.CG.Shared.FuncArgInfo[] FAI, G25.SMV returnType, string returnName)
{
G25.GMV gmv = S.m_GMV;
StringBuilder SB = new StringBuilder();
SB.AppendLine(FT.type + " n2 = " + FT.DoubleToString(S, 0.0) + ";");
if (S.OutputCSharpOrJava())
{
SB.AppendLine(FT.type + "[] c = new " + FT.type + "[1];");
SB.AppendLine(FT.type + "[][] ac = " + FAI[0].Name + ".to_" + FAI[0].MangledTypeName + "().c();");
}
else {
bool resultIsScalar = true;
bool initResultToZero = true;
SB.Append(GetExpandCode(S, cgd, FT, FAI, resultIsScalar, initResultToZero));
}
string exaName = "_" + FAI[0].Name;
for (int g1 = 0; g1 < gmv.NbGroups; g1++)
{
bool funcFound = false;
for (int g2 = 0; g2 < gmv.NbGroups; g2++) {
// get function name
string funcName = GetGPpartFunctionName(S, FT, M, g1, g2, 0); // grade 0 part of gp(g, g)
// check if funcName (gp part) exists)
Tuple<string, string, string> key = new Tuple<string, string, string>(FT.type, M.m_name, funcName);
if (cgd.m_gmvGPpartFuncNames.ContainsKey(key) && cgd.m_gmvGPpartFuncNames[key])
{
if (!funcFound)
{
if (S.OutputCSharpOrJava())
SB.Append("if (ac[" + g1 + "] != null) {");
else SB.Append("if (" + exaName + "[" + g1 + "] != NULL) { ");
SB.AppendLine(" /* group " + g1 + " (grade " + gmv.Group(g1)[0].Grade() + ") */");
SB.AppendLine("\tc[0] = " + FT.DoubleToString(S, 0.0) + ";");
funcFound = true;
}
if (g1 != g2)
{
if (S.OutputCSharpOrJava())
SB.Append("\tif (ac[" + g2 + "] != null) {");
else SB.Append("\tif (" + exaName + "[" + g2 + "] != NULL) {");
SB.AppendLine(" /* group " + g2 + " (grade " + gmv.Group(g2)[0].Grade() + ") */");
}
if (S.OutputCSharpOrJava())
SB.AppendLine("\t\t" + funcName + "(ac[" + g1 + "], ac[" + g2 + "], c);");
else SB.AppendLine("\t\t" + funcName + "(" + exaName + "[" + g1 + "], " + exaName + "[" + g2 + "], c);");
if (g1 != g2)
SB.AppendLine("\t}");
}
}
if (funcFound) // only do this if any matching gp function for this group was found
{
// get multiplier
double m = gmv.Group(g1)[0].Reverse().scale / gmv.Group(g1)[0].scale; // must always have the same grade
if (m == 1.0) SB.AppendLine("\tn2 += c[0];");
else if (m == -1.0) SB.AppendLine("\tn2 -= c[0];");
else SB.AppendLine("\tn2 += " + FT.DoubleToString(S, m) + " * c[0];");
SB.AppendLine("}");
}
}
string returnVal;
{ // get return value
if (squared) returnVal = "n2";
else
{
string sqrtFuncName = G25.CG.Shared.CodeUtil.OpNameToLangString(S, FT, RefGA.Symbolic.UnaryScalarOp.SQRT);
if (M.m_metric.IsPositiveDefinite()) // if PD, then negative values are impossible
returnVal = sqrtFuncName + "(n2)";
else returnVal = "((n2 < " + FT.DoubleToString(S, 0.0) + ") ? " + sqrtFuncName + "(-n2) : " + sqrtFuncName + "(n2))";
}
}
// can be shared with other GetNormCode
if ((returnType == null) || (returnName == null))
{
SB.AppendLine("return " + returnVal + ";");
}
else
{
if (S.OutputC())
{
// get assign code, assign it
bool mustCast = false;
bool dstPtr = true;
int nbTabs = 0;
bool writeZeros = true;
//.........这里部分代码省略.........
示例7: WriteFunctionShortcuts
/// <summary>
/// Writes all shortcuts for 'type'.
/// </summary>
/// <param name="SB">Where the code goes.</param>
/// <param name="S">Used for namespace.</param>
/// <param name="cgd">Not used yet.</param>
/// <param name="FT">Float point type of 'type'.</param>
/// <param name="type">The type for which the function should be written.</param>
public static void WriteFunctionShortcuts(StringBuilder SB, Specification S, G25.CG.Shared.CGdata cgd, FloatType FT, G25.VariableType type)
{
Dictionary<string, List<G25.Operator>> operatorMap = S.GetOperatorMap();
Dictionary<string, bool> boundOperators = new Dictionary<string, bool>();
foreach (G25.fgs fgs in S.m_functions)
{
if ((type is G25.SMV) && fgs.IsConverter(S))
{
G25.SMV smv = (G25.SMV)type;
if (fgs.IsConverterSource(S, (G25.SMV)type, FT))
{
// write converter here . . .
//SB.AppendLine("// converter source here!");
} else if (fgs.IsConverterDestination(S, smv, FT))
{
// write converter here . . .
Converter.WriteMemberConverter(SB, S, cgd, FT, fgs, (G25.SMV)S.GetType(fgs.ArgumentTypeNames[0]), smv);
}
}
else if (fgs.GetSupportedByPlugin() && (fgs.NbArguments >= 1) && (Array.IndexOf(fgs.FloatNames, FT.type) >= 0))
{
// get function arguments
bool computeMultivectorValue = false;
G25.CG.Shared.FuncArgInfo[] FAI = null;
try
{
FAI = G25.CG.Shared.FuncArgInfo.GetAllFuncArgInfo(S, fgs, fgs.NbArguments, FT, "not set", computeMultivectorValue);
}
catch (Exception ex)
{
if ((type is G25.GMV) && (FT == S.m_floatTypes[0])) // only warn once
Console.WriteLine("Warning: cannot generate shortcut to " + fgs.ToString());
continue;
}
// if type matches, write the shortcut, and possibly an operator
if (FAI[0].TypeName.Equals(type.GetName()))
{
WriteFunctionShortcut(SB, S, cgd, FT, type, fgs, FAI);
if (S.OutputCSharpOrJava())
removeMvInterfaces(FAI); // arguments to operators need to be of the multivector type, not the multivector interface type
WriteOperatorShortcut(SB, S, cgd, FT, type, fgs, FAI, operatorMap, boundOperators);
}
}
}
}
示例8: WriteSpecializedFunction
/// <summary>
/// Writes a simple function which assigns some value based on specialized multivectors or scalars to some variable.
///
/// Used by a lot of simple C functions, like gp, op, lc.
///
/// Somewhat obsolete (preferably, use the more generic, instruction based WriteFunction()).
///
/// </summary>
/// <param name="S">Specification of algebra.</param>
/// <param name="cgd">Results go into cgd.m_defSB, and so on</param>
/// <param name="F">The function generation specification.</param>
/// <param name="FT">Default float pointer type.</param>
/// <param name="FAI">Info on the arguments of the function.</param>
/// <param name="value">The value to assign.</param>
/// <param name="comment">Optional comment for function (can be null).</param>
public static void WriteSpecializedFunction(Specification S, G25.CG.Shared.CGdata cgd, G25.fgs F,
FloatType FT, G25.CG.Shared.FuncArgInfo[] FAI, RefGA.Multivector value, Comment comment)
{
// get return type (may be a G25.SMV or a G25.FloatType)
G25.VariableType returnType = G25.CG.Shared.SpecializedReturnType.GetReturnType(S, cgd, F, FT, value);
if (returnType == null)
throw new G25.UserException("Missing return type: " + G25.CG.Shared.BasisBlade.MultivectorToTypeDescription(S, value),
XML.FunctionToXmlString(S, F));
bool ptr = true;
string dstName = G25.fgs.RETURN_ARG_NAME;
//string dstTypeName = (returnType is G25.SMV) ? FT.GetMangledName((returnType as G25.SMV).Name) : (returnType as G25.FloatType).type;
string funcName = F.OutputName;
// write comment to declaration
if (comment != null)
{
if (S.OutputCppOrC())
comment.Write(cgd.m_declSB, S, 0);
else comment.Write(cgd.m_defSB, S, 0);
}
if ((returnType is G25.SMV) && (S.OutputC()))
{
bool mustCast = false;
G25.SMV dstSmv = returnType as G25.SMV;
G25.CG.Shared.FuncArgInfo returnArgument = null;
returnArgument = new G25.CG.Shared.FuncArgInfo(S, F, -1, FT, dstSmv.Name, false); // false = compute value
bool staticFunc = S.OutputCSharpOrJava();
G25.CG.Shared.Functions.WriteAssignmentFunction(S, cgd,
S.m_inlineFunctions, staticFunc, "void", null, funcName, returnArgument, FAI, FT, mustCast, dstSmv, dstName, ptr,
value);
}
else
{
G25.FloatType returnFT = ((returnType as G25.FloatType) == null) ? FT : (returnType as G25.FloatType);
bool mustCast = false;
for (int i = 0; i < FAI.Length; i++)
mustCast |= returnFT.MustCastIfAssigned(S, FAI[i].FloatType);
bool staticFunc = S.OutputCSharpOrJava();
G25.CG.Shared.Functions.WriteReturnFunction(S, cgd,
S.m_inlineSet, staticFunc, funcName, FAI, FT, mustCast, returnType, value);
}
}
示例9: GenerateReturnCode
public static string GenerateReturnCode(Specification S, G25.SMV smv, G25.FloatType FT, String[] valueStr, int nbTabs, bool writeZeros)
{
StringBuilder SB = new StringBuilder();
string smvName = FT.GetMangledName(S, smv.Name);
string STATIC_MEMBER_ACCESS = (S.OutputCpp()) ? "::" : ".";
SB.Append('\t', nbTabs);
SB.Append("return ");
if (S.OutputCSharpOrJava())
SB.Append("new ");
SB.Append(smvName);
SB.Append("(");
if (valueStr.Length > 0)
{
SB.AppendLine(smvName + STATIC_MEMBER_ACCESS + SmvUtil.GetCoordinateOrderConstant(S, smv) + ",");
}
for (int i = 0; i < valueStr.Length; i++) {
SB.Append('\t', nbTabs+2);
SB.Append(valueStr[i]);
if (i < (valueStr.Length - 1)) SB.Append(",");
SB.AppendLine(" // " + smv.NonConstBasisBlade(i).ToLangString(S.m_basisVectorNames) );
}
SB.Append('\t', nbTabs+1);
SB.Append(");");
return SB.ToString();
}
示例10: WriteDeclaration
/// <summary>
/// Writes a function declaration to 'SB'.
/// The closing comma is NOT included so the function can also be used as the start of a definition.
/// </summary>
/// <param name="SB">Where declaration goes.</param>
/// <param name="S">Used for all kinds of stuff.</param>
/// <param name="cgd">Results go into cgd.m_defSB, and so on</param>
/// <param name="inline">Should the function we inline?</param>
/// <param name="staticFunc">Static function?</param>
/// <param name="returnType">String which speficies the return type.</param>
/// <param name="functionName">The name of the function which is to be generated.</param>
/// <param name="returnArgument">FuncArgInfo which describes the optional return argument.</param>
/// <param name="arguments">Array of FuncArgInfo which describes the arguments of the function.</param>
public static void WriteDeclaration(StringBuilder SB, Specification S, G25.CG.Shared.CGdata cgd,
bool inline, bool staticFunc, string returnType, string functionName,
FuncArgInfo returnArgument, FuncArgInfo[] arguments)
{
if (S.OutputJava())
SB.Append("public final ");
else if (S.OutputCSharp())
SB.Append("public ");
if (staticFunc) SB.Append("static ");
SB.Append(G25.CG.Shared.Util.GetInlineString(S, inline, " "));
if (returnArgument != null) returnType = returnArgument.MangledTypeName + "*"; // maybe for C write returnType = returnArgument + "*"?
SB.Append(returnType);
SB.Append(" ");
SB.Append(functionName);
SB.Append("(");
{ // write arguments
bool appendComma = false;
int nbArgs = (arguments == null) ? 0 : arguments.Length;
for (int i = -1; i < nbArgs; i++) // start at -1 for return argument
{
FuncArgInfo A = null;
if (i == -1)
{
A = returnArgument;
if (A == null) continue;
}
else A = arguments[i];
if (appendComma) SB.Append(", ");
if (S.OutputJava())
SB.Append("final ");
if (A.Constant && S.OutputCppOrC())
SB.Append("const ");
SB.Append(A.MangledTypeName);
if (S.OutputCSharpOrJava() && A.IsGMV() && A.MvInterface)
SB.Append(Main.IF_SUFFIX);
if (A.Array && S.OutputCSharpOrJava())
SB.Append("[]");
SB.Append(" ");
if (A.Pointer) SB.Append("*");
else if (S.OutputCpp() && (A.IsMVorOM())) // append '&'?
SB.Append("&");
SB.Append(A.Name);
if (A.Array && S.OutputCppOrC())
SB.Append("[]");
appendComma = true;
}
}
SB.Append(")");
}
示例11: WriteFunction
/// <summary>
/// Writes generic function based on Instructions.
///
/// The other WriteFunction() can figure out the return type automatically, so
/// it is preferred over this more verbose version.
/// </summary>
/// <param name="S">Specification of algebra.</param>
/// <param name="cgd">Results go into cgd.m_defSB, and so on</param>
/// <param name="F">Function specification.</param>
/// <param name="inline">When true, the code is inlined.</param>
/// <param name="staticFunc">Static function?</param>
/// <param name="returnType">The type to return (String, can also be e.g. <c>"code"</c>.</param>
/// <param name="functionName">Name of generated function.</param>
/// <param name="returnArgument">For use with the 'C' language, an extra argument can be used to return results.</param>
/// <param name="arguments">Arguments of function (any `return argument' used for the C language is automatically generated).</param>
/// <param name="instructions">List of GA-instructions which make up the function.</param>
/// <param name="comment">Comment to go into generated code (used for decl only).</param>
/// <param name="writeDecl">When false, no declaration is written</param>
public static void WriteFunction(
Specification S, G25.CG.Shared.CGdata cgd, G25.fgs F,
bool inline, bool staticFunc, string returnType, string functionName,
FuncArgInfo returnArgument, FuncArgInfo[] arguments,
System.Collections.Generic.List<Instruction> instructions, Comment comment, bool writeDecl)
{
// where the definition goes:
StringBuilder defSB = (inline) ? cgd.m_inlineDefSB : cgd.m_defSB;
// declaration:
if (writeDecl)
{
if (comment != null) comment.Write(cgd.m_declSB, S, 0);
bool inlineDecl = false; // never put inline keywords in declaration
WriteDeclaration(cgd.m_declSB, S, cgd, inlineDecl, staticFunc, returnType, functionName, returnArgument, arguments);
cgd.m_declSB.AppendLine(";");
}
if (S.OutputCSharpOrJava()) comment.Write(defSB, S, 0);
WriteDeclaration(defSB, S, cgd, inline, staticFunc, returnType, functionName, returnArgument, arguments);
// open function
defSB.AppendLine("");
defSB.AppendLine("{");
// add extra instruction for reporting usage of SMVs
if (S.m_reportUsage)
instructions.Insert(0, ReportUsage.GetReportInstruction(S, F, arguments));
if (returnArgument != null)
{
int nbTabs = 1;
instructions.Add(new VerbatimCodeInstruction(nbTabs, "return " + returnArgument.Name + ";"));
}
// write all instructions
foreach (Instruction I in instructions)
{
I.Write(defSB, S, cgd);
}
// close function
defSB.AppendLine("}");
}
示例12: WriteAssignmentFunction
/// <summary>
/// Writes a function to 'SB' which assigns a certain 'value' to a certain 'dstName'.
/// </summary>
/// <param name="S">Used for all kinds of stuff.</param>
/// <param name="cgd">Results go into cgd.m_defSB, and so on</param>
/// <param name="inline">Should the function we inline?</param>
/// <param name="staticFunc">Static function?</param>
/// <param name="returnType">String which speficies the return type.</param>
/// <param name="returnVarName">The name of the variable which should be returned. Should be one of the argument names.</param>
/// <param name="functionName">The name of the function which is to be generated.</param>
/// <param name="arguments">Array of FuncArg which describes the arguments of the function.</param>
/// <param name="dstFT">Floating point type of destination variable.</param>
/// <param name="mustCastDst">set to true if coordinates of 'value' must be cast to 'dstFT'.</param>
/// <param name="dstSmv">G25.SMV type of destination.</param>
/// <param name="dstName">Name of destination.</param>
/// <param name="dstPtr">Is the destination a pointer?</param>
/// <param name="value">Value to be written to the destination.</param>
/// <param name="returnArgument">For use with the 'C' language, an extra argument can be used to return results.</param>
public static void WriteAssignmentFunction(
Specification S, G25.CG.Shared.CGdata cgd,
bool inline, bool staticFunc, string returnType, string returnVarName, string functionName,
FuncArgInfo returnArgument, FuncArgInfo[] arguments,
FloatType dstFT, bool mustCastDst, G25.SMV dstSmv, string dstName, bool dstPtr, RefGA.Multivector value)
{
// where the definition goes:
StringBuilder defSB = (inline) ? cgd.m_inlineDefSB : cgd.m_defSB;
// write declaration yes/no?
bool writeDecl = (!(dstName.Equals(G25.CG.Shared.SmvUtil.THIS) && S.OutputCpp())) && // no declarations for C++ member functions
(!S.OutputCSharpOrJava()); // no declarations in C# and Java
if (writeDecl)
{
WriteDeclaration(cgd.m_declSB, S, cgd, inline, staticFunc, returnType, functionName, returnArgument, arguments);
cgd.m_declSB.AppendLine(";");
}
WriteDeclaration(defSB, S, cgd, inline, staticFunc, returnType, functionName, returnArgument, arguments);
defSB.AppendLine("");
defSB.AppendLine("{");
int nbTabs = 1;
bool declareVariable = false;
AssignInstruction AI = new AssignInstruction(nbTabs, dstSmv, dstFT, mustCastDst, value, dstName, dstPtr, declareVariable);
AI.Write(defSB, S, cgd);
if (returnVarName != null)
{
defSB.AppendLine("\treturn " + returnVarName + ";");
} else if (returnArgument != null) {
defSB.AppendLine("\treturn " + returnArgument.Name + ";");
}
defSB.AppendLine("}");
}
示例13: OutputStaticFunctions
/// <summary>
/// Returns true for C# and Java. This is used to determine whether the
/// user functions are all static (because they must be put in class instead of a namespace).
/// </summary>
public static bool OutputStaticFunctions(Specification S)
{
return (S.OutputCSharpOrJava());
}
示例14: GetReportInstruction
// add someextra instructions for report usage?
// add extra verbatim code here if rep usage?
// what arguments are required??
public static Instruction GetReportInstruction(Specification S, G25.fgs F, FuncArgInfo[] FAI)
{
if ((S.OutputC()) ||
(!S.m_reportUsage) ||
(FAI.Length == 0)) return new NOPinstruction();
// check if all arguments are GMVs
for (int i = 0; i < FAI.Length; i++)
{
if (!FAI[i].IsGMV()) return new NOPinstruction();
}
// get XML spec
string XMLstr = GetXMLstring(S, F, FAI);
StringBuilder SB = new StringBuilder();
if (S.OutputCSharpOrJava())
{
for (int i = 0; i < FAI.Length; i++)
{
SB.AppendLine("SmvType type_" + FAI[i].Name + " = " + FAI[i].Name + ".to_" + FAI[i].MangledTypeName +"().m_t;");
}
}
{
string MV_CONSTANT = GetSpecializedConstantName(S, S.m_GMV.Name);
string INVALID_CONSTANT = GetSpecializedConstantName(S, INVALID);
// output the test for all specialized MVs
SB.Append("if (");
for (int i = 0; i < FAI.Length; i++)
{
if (i > 0)
{
SB.AppendLine(" && ");
SB.Append("\t");
}
if (S.OutputCpp())
{
SB.Append("(" + FAI[i].Name + ".m_t > " + MV_CONSTANT + ") && (" + FAI[i].Name + ".m_t < " + INVALID_CONSTANT + ")");
}
else if (S.OutputCSharp())
{
SB.Append("(type_" + FAI[i].Name + " > SmvType." + MV_CONSTANT + ") && (type_" + FAI[i].Name + " < SmvType." + INVALID_CONSTANT + ")");
}
else if (S.OutputJava())
{
SB.Append("(type_" + FAI[i].Name + ".compareTo(SmvType." + MV_CONSTANT + ") > 0) && (type_" + FAI[i].Name + ".compareTo(SmvType." + INVALID_CONSTANT + ") < 0)");
}
}
SB.AppendLine(") {");
if (S.OutputCpp())
{
SB.Append("\t\tstd::string reportUsageString = std::string(\"\") + ");
}
else if (S.OutputCSharp())
{
SB.Append("\t\tstring reportUsageString = ");
}
else if (S.OutputJava())
{
SB.Append("\t\tString reportUsageString = ");
}
// output XMLstr, replace placeholders with code
int XMLstrIdx = 0;
int argIdx = 0;
while (XMLstrIdx < XMLstr.Length)
{
string placeHolder = GetPlaceHolderString(argIdx);
int nextIdx = XMLstr.IndexOf(placeHolder, XMLstrIdx);
if (nextIdx < 0) nextIdx = XMLstr.Length;
SB.Append(Util.StringToCode(XMLstr.Substring(XMLstrIdx, nextIdx - XMLstrIdx)));
if (argIdx < FAI.Length)
{
if (S.OutputCpp())
{
SB.Append("+ g_" + S.m_namespace + "Typenames[" + FAI[argIdx].Name + ".m_t] + ");
}
else if (S.OutputCSharp())
{
SB.Append("+ typenames[(int)type_" + FAI[argIdx].Name + "] + ");
}
else if (S.OutputJava())
{
SB.Append("+ typenames[type_" + FAI[argIdx].Name + ".getId()] + ");
}
}
argIdx++;
XMLstrIdx = nextIdx + placeHolder.Length;
}
SB.AppendLine(";");
if (S.OutputCpp())
{
//.........这里部分代码省略.........
示例15: GetVersorApplicationCode
/// <summary>
/// Returns the code for applying a versor <c>V</c>to some multivector <c>M</c> (V M / V).
/// The code is composed of calls to functions generated by <c>WriteGmvGpParts()</c>.
///
/// This function does not explicitly use the geometric product parts code, but makes calls
/// to the geometric product and the reverse/versor inverse. As such it generates dependencies
/// on <c>gp</c>, <c>reverse</c>, <c>versorInverse</c> and <c>grade</c>.
///
/// Three types of code can be generated, depending on the value of
/// argument <c>T</c>:
/// - <c>REVERSE</c>: the versor is unit, so the reverse can be used.
/// - <c>INVERSE</c>: the versor inverse is used to compute the inverse.
/// - <c>EXPLICIT_INVERSE</c>: the inverse is explicitly provided.
///
/// 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">Used for resolving dependencies.</param>
/// <param name="FT">Floating point type.</param>
/// <param name="M">Metric type used in the geometric products.</param>
/// <param name="T">The product (e.g., geometric, outer, etc).</param>
/// <param name="FAI">Info about function arguments. All arguments must be general multivectors.</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 GetVersorApplicationCode(Specification S, G25.CG.Shared.CGdata cgd,
G25.FloatType FT, G25.Metric M, ApplyVersorTypes T,
G25.CG.Shared.FuncArgInfo[] FAI, String resultName)
{
G25.GMV gmv = S.m_GMV;
StringBuilder SB = new StringBuilder();
// get geometric product function
string gpFuncName = G25.CG.Shared.Dependencies.GetDependency(S, cgd, "gp", new String[] { gmv.Name, gmv.Name }, FT, M.m_name);
// get reverse or inverse function
string invFuncName = null;
if ((T == ApplyVersorTypes.INVERSE) || (T == ApplyVersorTypes.REVERSE))
{
string inputFuncName = (T == ApplyVersorTypes.INVERSE) ? "versorInverse" : "reverse";
invFuncName = G25.CG.Shared.Dependencies.GetDependency(S, cgd,
inputFuncName, new string[] { gmv.Name }, FT, (T == ApplyVersorTypes.INVERSE) ? M.m_name : null);
}
// get grade function
string gradeFuncName = G25.CG.Shared.Dependencies.GetDependency(S, cgd, G25.CG.Shared.CANSparts.EXTRACT_GRADE, new String[] { gmv.Name, G25.GroupBitmapType.GROUP_BITMAP }, FT, null);
// get string to be used for grade extraction
string gradeUsageString;
string bgu;
if (S.OutputC()) {
bgu = FAI[1].Name + "->gu";
}
else if (S.OutputCSharpOrJava()) {
bgu = FAI[1].Name + ".to_" + FAI[1].MangledTypeName + "().gu()";
}
else {
bgu = FAI[1].Name + ".gu()";
}
string groupBitmapType = (S.OutputCSharp() ? "GroupBitmap" : "int");
if (!gmv.IsGroupedByGrade(S.m_dimension))
{
SB.AppendLine(groupBitmapType + " gradeUsageBitmap;");
gradeUsageString = "gradeUsageBitmap";
}
else
{
gradeUsageString = bgu;
/*if (S.OutputC())
gradeUsageString = FAI[1].Name + "->gu";
else gradeUsageString = FAI[1].Name + ".gu()";*/
}
// select grade parts!
StringBuilder gradeUsageCode = new StringBuilder();
if (!gmv.IsGroupedByGrade(S.m_dimension))
{
string groupBitmapStr = (S.OutputCSharpOrJava() ? ("GroupBitmap.") : "");
gradeUsageCode.Append(gradeUsageString + " = ");
for (int g = 0; g <= S.m_dimension; g++)
{
if (g > 0) gradeUsageCode.Append(" | ");
gradeUsageCode.Append("(((" + bgu + " & " + groupBitmapStr + "GRADE_" + g + ") != 0) ? " + groupBitmapStr + "GRADE_" + g + " : 0)");
}
gradeUsageCode.AppendLine(";");
}
if (S.OutputC())
{
// get name of inverse, decl tmp variable for inverse if required
string inverseInputName;
if (T == ApplyVersorTypes.EXPLICIT_INVERSE) inverseInputName = FAI[2].Name;
else if (T == ApplyVersorTypes.INVERSE)
{
inverseInputName = "&inv";
SB.AppendLine(FT.GetMangledName(S, gmv.Name) + " inv; /* temp space for reverse */");
}
else // (T == ApplyVersorTypes.REVERSE)
//.........这里部分代码省略.........