(* Content-type: application/mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 6.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 145, 7] NotebookDataLength[ 275563, 7240] NotebookOptionsPosition[ 274431, 7200] NotebookOutlinePosition[ 274878, 7219] CellTagsIndexPosition[ 274835, 7216] WindowFrame->Normal ContainsDynamic->False*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["SeQuant: toolkit for second quantized expressions", "Title"], Cell[TextData[{ "\nThis is a collection of tools that assist in manipulating expressions \ encoutered in second quantized form of quantum mechanics\n\nUser-level \ functions:\n createParticleIndex[a_String, b_particleSpace]\t\t\tcreate \ particleIndex object describing index \"a\" that belongs to space b\n \ createSQS[cre_List, ann_List]\t\t\t\t\tcreate SQ strings of creation(cre) and \ annihilation(ann) operators (in normal form)\n createSQM[O_String, \ bra_List, ket_List, psym_indexSymm]\tcreate SQ matrix elements of operator \ \"O\" between bra and ket. Permutational symmetry of the operator\n \t\t\t\ \t\t\t\t\t\tis described by psym\n wick[expr_, extInds_List,]\t\t\ \t\tapply Wick's theorem to expr with options specified by opt. extInds is a \ list of ", StyleBox["external", FontSlant->"Italic"], " indices (i.e. over\n \t\t\t\t\t\t\t\t\twhich not to perform summation. \ The argument opt is optional\n \t\t\t\t\t\t\t\t\tDefault options:\n \ \t\t\t\t\t\t\t\t\tfullContract->True\t\tonly leave fully contracted terms \ (set to False to compute matrix elements)\n \t\t\t\t\t\t\t\t\t\ noCoincidences->False\tthere may be coincidences between external indices \ (should be set\n \t\t\t\t\t\t\t\t\t\t\t\t\tto True if less general \ expressions for matrix elements are to be obtained)\n \t\t\t\t\t\t\t\t\t\ doSums->True\t\t\tcarry out summation over ", StyleBox["internal", FontSlant->"Italic"], " indices (indices present in\n \t\t\t\t\t\t\t\t\t\t\t\t\texpr and not \ found in extInds)\n \t\t\t\t\t\t\t\t\tdoReindex->True\t\tsimplify \ further the expression by reindexing internal indices into\n \t\t\t\t\t\t\ \t\t\t\t\t\t\tcanonical order\n \t\t\t\t\t\t\t\t\t" }], "Text"], Cell[CellGroupData[{ Cell["Here comes the code -- execute this cell to load the package", "Section"], Cell[BoxData[ RowBox[{ RowBox[{"(*", RowBox[{ RowBox[{"BeginPackage", "[", "\"\\"", "]"}], ";"}], "*)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", " ", RowBox[{"Setting", " ", "up", " ", "internal", " ", "things"}], " ", "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"Off", "[", RowBox[{"General", "::", "spell"}], "]"}], ";", RowBox[{"Off", "[", RowBox[{"General", "::", "spell1"}], "]"}], ";", RowBox[{"$RecursionLimit", "=", "8192"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"$IterationLimit", "=", "Infinity"}], ";"}], "\[IndentingNewLine]", RowBox[{"(*", RowBox[{ RowBox[{"SetOptions", "[", RowBox[{"$FrontEnd", ",", RowBox[{"CommonDefaultFormatTypes", "\[Rule]", RowBox[{ StyleBox["{", "MR"], RowBox[{"\"\\"", "\[Rule]", "TraditionalForm"}], "}"}]}]}], "]"}], ";"}], "*)"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"NCM", "=", "NonCommutativeMultiply"}], ";"}], StyleBox["\[IndentingNewLine]", "Commentary"], StyleBox["\[IndentingNewLine]", "Commentary"], StyleBox[ RowBox[{"(*", "\[IndentingNewLine]", RowBox[{"Special", " ", "Operators"}], "\[IndentingNewLine]", "*)"}], "Commentary"], StyleBox["\[IndentingNewLine]", "Commentary"], RowBox[{ RowBox[{"Unprotect", "[", "defaultHamiltonianOpers", "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Unprotect", "[", "ignoreDisconnectedOpers", "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Unprotect", "[", "ignoreConnectedOpers", "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"defaultHamiltonianOpers", "=", RowBox[{"{", RowBox[{ "\"\\"", ",", "\"\<\!\(\*OverscriptBox[\(g\), \(_\)]\)\>\""}], "}"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"ignoreDisconnectedOpers", "=", RowBox[{"{", "}"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"ignoreConnectedOpers", "=", RowBox[{"{", "}"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Protect", "[", "ignoreConnectedOpers", "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Protect", "[", "ignoreDisconnectedOpers", "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Protect", "[", "defaultHamiltonianOpers", "]"}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", "\[IndentingNewLine]", RowBox[{"Global", " ", "SeQuant", " ", "Parameters"}], "\[IndentingNewLine]", "*)"}], "Commentary"], StyleBox["\[IndentingNewLine]", "Commentary"], RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{"!", RowBox[{"ValueQ", "[", "SeQuantDebugLevel", "]"}]}], ",", RowBox[{"SeQuantDebugLevel", "=", "0"}]}], "]"}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", " ", RowBox[{"Definition", " ", "of", " ", "spaces"}], " ", "*)"}], "Commentary"], StyleBox["\[IndentingNewLine]", "Commentary"], RowBox[{ RowBox[{"occ", " ", "=", " ", RowBox[{"particleSpace", "[", "occupied", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"virt", " ", "=", " ", RowBox[{"particleSpace", "[", "virtual", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"othervirt", " ", "=", " ", RowBox[{"particleSpace", "[", "othervirtual", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"any", " ", "=", " ", RowBox[{"Union", "[", RowBox[{"occ", ",", "virt"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"allvirt", " ", "=", RowBox[{"Union", "[", " ", RowBox[{"virt", ",", "othervirt"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"allany", " ", "=", " ", RowBox[{"Union", "[", RowBox[{"occ", ",", "allvirt"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"spacesOverlapIgnoreParticleType", "[", RowBox[{"a_particleSpace", ",", "b_particleSpace"}], "]"}], ":=", RowBox[{"(", RowBox[{ RowBox[{"Select", "[", RowBox[{ RowBox[{"Intersection", "[", RowBox[{"a", ",", "b"}], "]"}], ",", RowBox[{ RowBox[{ RowBox[{"Head", "[", "#", "]"}], "=!=", "particleType"}], "&"}]}], "]"}], "=!=", RowBox[{"particleSpace", "[", "]"}]}], ")"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"spacesOverlap", "[", RowBox[{"a_particleSpace", ",", "b_particleSpace"}], "]"}], ":=", RowBox[{"(", RowBox[{ RowBox[{"spacesOverlapIgnoreParticleType", "[", RowBox[{"a", ",", "b"}], "]"}], " ", "&&", " ", RowBox[{ RowBox[{"Select", "[", RowBox[{"a", ",", RowBox[{ RowBox[{ RowBox[{"Head", "[", "#", "]"}], "===", "particleType"}], "&"}]}], "]"}], "===", RowBox[{"Select", "[", RowBox[{"b", ",", RowBox[{ RowBox[{ RowBox[{"Head", "[", "#", "]"}], "===", "particleType"}], "&"}]}], "]"}]}]}], ")"}]}], ";"}], StyleBox["\[IndentingNewLine]", "Commentary"], StyleBox[ RowBox[{"(*", " ", RowBox[{ RowBox[{"return", " ", "+", RowBox[{"1", " ", "if", " ", "above"}]}], ",", " ", RowBox[{ RowBox[{"-", "1"}], " ", "if", " ", "below"}], ",", " ", RowBox[{"0", " ", "if", " ", "neither", " ", "or", " ", "both"}]}], " ", "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"spaceWRTFermiLevel", "[", "a_particleSpace", "]"}], ":=", RowBox[{"If", "[", RowBox[{ RowBox[{"spacesOverlapIgnoreParticleType", "[", RowBox[{"a", ",", "allvirt"}], "]"}], ",", RowBox[{"If", "[", RowBox[{ RowBox[{"spacesOverlapIgnoreParticleType", "[", RowBox[{"a", ",", "occ"}], "]"}], ",", "0", ",", RowBox[{"+", "1"}]}], "]"}], ",", RowBox[{"If", "[", RowBox[{ RowBox[{"spacesOverlapIgnoreParticleType", "[", RowBox[{"a", ",", "occ"}], "]"}], ",", RowBox[{"-", "1"}], ",", "0"}], "]"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", " ", RowBox[{ RowBox[{ "Global", " ", "index", " ", "for", " ", "most", " ", "recently", " ", "generated", " ", "index", " ", "\[IndentingNewLine]", "Generated", " ", "indices", " ", "are", " ", "named", " ", "iXX"}], ",", " ", RowBox[{ "where", " ", "i", " ", "is", " ", "generated", " ", "by", " ", "DefaultSpaceSymbol"}]}], " ", "*)"}], "Commentary"], StyleBox["\[IndentingNewLine]", "Commentary"], RowBox[{ RowBox[{"GlobalIndexCounter", "=", "0"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"DefaultSpaceSymbol", "[", "occ", "]"}], "=", "\"\\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"DefaultSpaceSymbol", "[", "virt", "]"}], "=", "\"\\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"DefaultSpaceSymbol", "[", "allvirt", "]"}], "=", "\"\<\[Alpha]\>\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"DefaultSpaceSymbol", "[", "othervirt", "]"}], "=", "\"\<\[Alpha]'\>\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"DefaultSpaceSymbol", "[", "any", "]"}], "=", "\"\\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"DefaultSpaceSymbol", "[", "allany", "]"}], "=", "\"\<\[Kappa]\>\""}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", "\[IndentingNewLine]", " ", RowBox[{"particleIndex", " ", "class"}], "\[IndentingNewLine]", " ", "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"createParticleIndex", "[", RowBox[{"i_String", ",", "space_particleSpace"}], "]"}], " ", ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", "}"}], ",", RowBox[{"particleIndex", "[", RowBox[{"i", ",", "space"}], "]"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"indexQ", "[", RowBox[{"a_", ",", "b_particleIndex"}], "]"}], ":=", RowBox[{"MemberQ", "[", RowBox[{"a", ",", RowBox[{"c_particleIndex", "/;", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"c", "[", RowBox[{"[", "1", "]"}], "]"}], "\[Equal]", RowBox[{"b", "[", RowBox[{"[", "1", "]"}], "]"}]}], ")"}], "&&", RowBox[{"(", RowBox[{ RowBox[{"c", "[", RowBox[{"[", "2", "]"}], "]"}], "\[Equal]", RowBox[{"b", "[", RowBox[{"[", "2", "]"}], "]"}]}], ")"}]}]}], ",", "Infinity"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"indexCreQ", "[", "a_particleIndex", "]"}], ":=", RowBox[{"MemberQ", "[", RowBox[{"a", ",", RowBox[{"indexType", "[", "cre", "]"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"indexAnnQ", "[", "a_particleIndex", "]"}], ":=", RowBox[{"MemberQ", "[", RowBox[{"a", ",", RowBox[{"indexType", "[", "ann", "]"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"indexBraQ", "[", "a_particleIndex", "]"}], ":=", RowBox[{"MemberQ", "[", RowBox[{"a", ",", RowBox[{"indexType", "[", "bra", "]"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"indexKetQ", "[", "a_particleIndex", "]"}], ":=", RowBox[{"MemberQ", "[", RowBox[{"a", ",", RowBox[{"indexType", "[", "ket", "]"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"indexType", "[", "a_particleIndex", "]"}], ":=", RowBox[{ RowBox[{"Cases", "[", RowBox[{"a", ",", "_indexType"}], "]"}], "[", RowBox[{"[", RowBox[{"1", ",", "1"}], "]"}], "]"}]}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"indexSpace", "[", "a_particleIndex", "]"}], ":=", RowBox[{ RowBox[{"Cases", "[", RowBox[{"a", ",", "_particleSpace"}], "]"}], "[", RowBox[{"[", "1", "]"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"indexParticle", "[", "a_particleIndex", "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", "typeList", "}"}], ",", RowBox[{ RowBox[{"typeList", "=", RowBox[{"Cases", "[", RowBox[{ RowBox[{"a", "[", RowBox[{"[", "2", "]"}], "]"}], ",", "_particleType"}], "]"}]}], ";", RowBox[{"If", "[", RowBox[{ RowBox[{"typeList", "\[Equal]", RowBox[{"{", "}"}]}], ",", RowBox[{"Return", "[", RowBox[{"particleType", "[", "default", "]"}], "]"}], ",", RowBox[{"Return", "[", RowBox[{"typeList", "[", RowBox[{"[", "1", "]"}], "]"}], "]"}]}], "]"}]}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"indexSymbol", "[", "a_particleIndex", "]"}], ":=", RowBox[{"a", "[", RowBox[{"[", "1", "]"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"indexLight", "[", "a_particleIndex", "]"}], ":=", RowBox[{"createParticleIndex", "[", RowBox[{ RowBox[{"indexSymbol", "[", "a", "]"}], ",", RowBox[{"indexSpace", "[", "a", "]"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"indexEquiv", "[", RowBox[{"a_particleIndex", ",", "b_particleIndex"}], "]"}], ":=", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"a", "[", RowBox[{"[", "1", "]"}], "]"}], "===", RowBox[{"b", "[", RowBox[{"[", "1", "]"}], "]"}]}], ")"}], "&&", RowBox[{"(", RowBox[{ RowBox[{"a", "[", RowBox[{"[", "2", "]"}], "]"}], "===", RowBox[{"b", "[", RowBox[{"[", "2", "]"}], "]"}]}], ")"}]}]}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{ RowBox[{"Unprotect", "[", "Equal", "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"Equal", "[", RowBox[{"a_particleIndex", ",", "b_particleIndex"}], "]"}], ":=", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"a", "[", RowBox[{"[", "1", "]"}], "]"}], "===", RowBox[{"b", "[", RowBox[{"[", "1", "]"}], "]"}]}], ")"}], "&&", RowBox[{"(", RowBox[{ RowBox[{"a", "[", RowBox[{"[", "2", "]"}], "]"}], "===", RowBox[{"b", "[", RowBox[{"[", "2", "]"}], "]"}]}], ")"}]}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Protect", "[", "Equal", "]"}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", " ", RowBox[{ "this", " ", "functions", " ", "throws", " ", "out", " ", "all", " ", "multiple", " ", "occurences", " ", "of", " ", "the", " ", "same", " ", "index", " ", "from", " ", "inds"}], " ", "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"uniqueIndexList", "[", "inds_List", "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"tmpinds", ",", "ind"}], "}"}], ",", "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", " ", RowBox[{"Old", " ", "code"}], " ", "*)"}], "Commentary"], StyleBox["\[IndentingNewLine]", "Commentary"], RowBox[{"(*", RowBox[{ RowBox[{"tmpinds", "=", RowBox[{"{", RowBox[{"inds", "[", RowBox[{"[", "1", "]"}], "]"}], "}"}]}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"ind", "=", RowBox[{"inds", "[", RowBox[{"[", "i", "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"!", RowBox[{"indexQ", "[", RowBox[{"tmpinds", ",", "ind"}], "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"tmpinds", "=", RowBox[{"Append", "[", RowBox[{"tmpinds", ",", "ind"}], "]"}]}]}], "\[IndentingNewLine]", "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"i", ",", RowBox[{"Length", "[", "inds", "]"}]}], "}"}]}], "\[IndentingNewLine]", "]"}], ";"}], "*)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", " ", RowBox[{"Union", " ", "does", " ", "it", " ", "now"}], " ", "*)"}], "Commentary"], StyleBox["\[IndentingNewLine]", "Commentary"], RowBox[{ RowBox[{"tmpinds", "=", RowBox[{"Union", "[", "inds", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "tmpinds", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", " ", RowBox[{ RowBox[{"\"\<\>", "tag", "\"\<\>"}], " ", "and", " ", RowBox[{"\"\<\>", "untag", "\"\<\>"}], " ", "all", " ", "indices", " ", "in", " ", "expr", " ", "which", " ", "also", " ", "appear", " ", "in", " ", RowBox[{"intInds", ".", " ", "uses"}], " ", "indexQ"}], " ", "*)"}], "Commentary"], StyleBox["\[IndentingNewLine]", "Commentary"], RowBox[{ RowBox[{ RowBox[{"tagIndex", "[", "index_particleIndex", "]"}], ":=", RowBox[{"Append", "[", RowBox[{"index", ",", RowBox[{"mysecrettag", "[", "]"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"tagIndex", "[", "expr_", "]"}], ":=", "expr"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"untagIndex", "[", "index_particleIndex", "]"}], ":=", RowBox[{"DeleteCases", "[", RowBox[{"index", ",", "_mysecrettag"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"untagIndex", "[", "expr_", "]"}], ":=", "expr"}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"tagIndices", "[", RowBox[{"expr_", ",", "inds_List"}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"result", ",", "ninds", ",", "pos", ",", "ind"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"ninds", "=", RowBox[{"Length", "[", "inds", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", "expr"}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"ind", "=", RowBox[{"inds", "[", RowBox[{"[", "i", "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"pos", "=", RowBox[{"Position", "[", RowBox[{"result", ",", RowBox[{"c_particleIndex", "/;", RowBox[{"indexEquiv", "[", RowBox[{"c", ",", "ind"}], "]"}]}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"MapAt", "[", RowBox[{"tagIndex", ",", "result", ",", "pos"}], "]"}]}]}], "\[IndentingNewLine]", ",", RowBox[{"{", RowBox[{"i", ",", "1", ",", "ninds"}], "}"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "result", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"untagIndices", "[", RowBox[{"expr_", ",", "inds_List"}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"result", ",", "ninds", ",", "pos", ",", "ind"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"ninds", "=", RowBox[{"Length", "[", "inds", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", "expr"}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"ind", "=", RowBox[{"inds", "[", RowBox[{"[", "i", "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"pos", "=", RowBox[{"Position", "[", RowBox[{"result", ",", RowBox[{"c_particleIndex", "/;", RowBox[{"indexEquiv", "[", RowBox[{"c", ",", "ind"}], "]"}]}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"MapAt", "[", RowBox[{"untagIndex", ",", "result", ",", "pos"}], "]"}]}]}], "\[IndentingNewLine]", ",", RowBox[{"{", RowBox[{"i", ",", "1", ",", "ninds"}], "}"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "result", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", "\[IndentingNewLine]", " ", RowBox[{ "SQS", " ", "class", "\[IndentingNewLine]", "\[IndentingNewLine]", "SQS", " ", "is", " ", "a", " ", "string", " ", "of", " ", RowBox[{"creation", "/", "annihilation"}], " ", "operators", "\[IndentingNewLine]", "only", " ", "normal", " ", "ordered", " ", "strings", " ", "can", " ", "be", " ", "utilized", " ", "at", " ", "the", " ", "moment"}], "\[IndentingNewLine]", " ", "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{ RowBox[{"inorder", " ", "=", " ", RowBox[{"normalOrder", "[", "True", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"noorder", " ", "=", " ", RowBox[{"normalOrder", "[", "False", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"createSQS", "[", RowBox[{"creInds_List", ",", "annInds_List", ",", RowBox[{"norm_normalOrder:", "inorder"}]}], "]"}], " ", ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", "}"}], ",", RowBox[{"Return", "[", RowBox[{"flattenSQS", "[", RowBox[{"SQS", "[", RowBox[{"creInds", ",", "annInds", ",", "norm"}], "]"}], "]"}], "]"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"SQS", "::", "wrongdepth"}], "=", "\"\\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"flattenSQS", "[", "a_SQS", "]"}], " ", ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{ "nc", ",", "na", ",", "result", ",", "creInds", ",", "annInds", ",", "x"}], "}"}], ",", RowBox[{ RowBox[{"Off", "[", RowBox[{"Append", "::", "normal"}], "]"}], ";", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"Depth", "[", "a", "]"}], "<", "5"}], ",", RowBox[{"Return", "[", "a", "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Clear", "[", "result", "]"}], ";", RowBox[{"Clear", "[", "creInds", "]"}], ";", RowBox[{"Clear", "[", "annInds", "]"}], ";", RowBox[{"Clear", "[", "x", "]"}], ";", RowBox[{"na", "=", RowBox[{"Length", "[", RowBox[{"a", "[", RowBox[{"[", "2", "]"}], "]"}], "]"}]}], ";", RowBox[{"nc", "=", RowBox[{"Length", "[", RowBox[{"a", "[", RowBox[{"[", "1", "]"}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"creInds", "=", RowBox[{"Cases", "[", RowBox[{ RowBox[{"a", "[", RowBox[{"[", "1", "]"}], "]"}], ",", RowBox[{"x_particleIndex", "\[Rule]", RowBox[{"Append", "[", RowBox[{"x", ",", RowBox[{"indexType", "[", "cre", "]"}]}], "]"}]}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"annInds", "=", RowBox[{"Cases", "[", RowBox[{ RowBox[{"a", "[", RowBox[{"[", "2", "]"}], "]"}], ",", RowBox[{"x_particleIndex", "\[Rule]", RowBox[{"Append", "[", RowBox[{"x", ",", RowBox[{"indexType", "[", "ann", "]"}]}], "]"}]}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"FlattenAt", "[", RowBox[{ RowBox[{"SQS", "[", RowBox[{"FlattenAt", "[", RowBox[{ RowBox[{"{", RowBox[{"creInds", ",", RowBox[{"Reverse", "[", "annInds", "]"}]}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"{", "1", "}"}], ",", RowBox[{"{", "2", "}"}]}], "}"}]}], "]"}], "]"}], ",", RowBox[{"{", "1", "}"}]}], "]"}]}], ";", RowBox[{"Return", "[", "result", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"indexTypesSQS", "[", "a_SQS", "]"}], " ", ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"ninds", ",", "ind", ",", "types", ",", "type"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"ninds", "=", RowBox[{"Length", "[", "a", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"types", "=", RowBox[{"{", "}"}]}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"type", "=", RowBox[{"indexParticle", "[", RowBox[{"a", "[", RowBox[{"[", "ind", "]"}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"types", "=", RowBox[{"Append", "[", RowBox[{"types", ",", "type"}], "]"}]}]}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"ind", ",", "1", ",", "ninds"}], "}"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"types", "=", RowBox[{"Union", "[", RowBox[{"Flatten", "[", "types", "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "types", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"numIndicesOfType", "[", RowBox[{"a_SQS", ",", "ptype_particleType"}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"ninds", ",", "result"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"ninds", "=", RowBox[{"Length", "[", "a", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", "0"}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", RowBox[{ RowBox[{"result", "+=", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"indexParticle", "[", RowBox[{"a", "[", RowBox[{"[", "i", "]"}], "]"}], "]"}], "===", "ptype"}], ",", "1", ",", "0"}], "]"}]}], ",", RowBox[{"{", RowBox[{"i", ",", "1", ",", "ninds"}], "}"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "result", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", RowBox[{ RowBox[{ "Find", " ", "the", " ", "first", " ", "index", " ", "of", " ", "ptype"}], ",", " ", RowBox[{ RowBox[{"else", " ", "return"}], " ", "-", "1"}]}], "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"firstIndexOfType", "[", RowBox[{"a_SQS", ",", "ptype_particleType"}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"ninds", ",", "ind", ",", "index"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"ninds", "=", RowBox[{"Length", "[", "a", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"ind", "=", RowBox[{"-", "1"}]}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"index", "=", RowBox[{"a", "[", RowBox[{"[", "i", "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"indexParticle", "[", "index", "]"}], "===", "ptype"}], ",", RowBox[{ RowBox[{"ind", "=", "i"}], ";", RowBox[{"Break", "[", "]"}]}]}], "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"i", ",", "1", ",", "ninds"}], "}"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "ind", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", "\[IndentingNewLine]", " ", RowBox[{ RowBox[{ "SQM", " ", "class", "\[IndentingNewLine]", "\[IndentingNewLine]", "SQM", " ", "is", " ", "a", " ", "matrix", " ", "element", " ", "of", " ", "an", " ", "operator", "\[IndentingNewLine]", "operators", " ", "can", " ", "be", " ", "classified", " ", "as", " ", "antisymm"}], ",", " ", "symm", ",", " ", RowBox[{"and", " ", "nonsymmetric"}], ",", " ", RowBox[{ "which", " ", "will", " ", "help", " ", "in", " ", "reducing", " ", "expressions", " ", "to", " ", "their", " ", "simplest", " ", "form"}]}], "\[IndentingNewLine]", " ", "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{ RowBox[{"antisymm", "=", RowBox[{"indexSymm", "[", RowBox[{"-", "1"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"symm", "=", RowBox[{"indexSymm", "[", "1", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"nonsymm", "=", RowBox[{"indexSymm", "[", "0", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"createSQM", "[", RowBox[{"O_String", ",", "braInds_List", ",", "ketInds_List", ",", RowBox[{"symm_indexSymm:", "antisymm"}]}], "]"}], " ", ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", "}"}], ",", RowBox[{"Return", "[", RowBox[{"flattenSQM", "[", RowBox[{"SQM", "[", RowBox[{"O", ",", "braInds", ",", "ketInds", ",", "symm"}], "]"}], "]"}], "]"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"SQM", "::", "wrongdepth"}], "=", "\"\\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"flattenSQM", "[", "a_SQM", "]"}], " ", ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{ "nb", ",", "nk", ",", "result", ",", "braInds", ",", "ketInds", ",", "x"}], "}"}], ",", RowBox[{ RowBox[{"Off", "[", RowBox[{"Append", "::", "normal"}], "]"}], ";", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"Depth", "[", "a", "]"}], "<", "5"}], ",", RowBox[{"Return", "[", "a", "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Clear", "[", "result", "]"}], ";", RowBox[{"Clear", "[", "braInds", "]"}], ";", RowBox[{"Clear", "[", "ketInds", "]"}], ";", RowBox[{"Clear", "[", "x", "]"}], ";", RowBox[{"nb", "=", RowBox[{"Length", "[", RowBox[{"a", "[", RowBox[{"[", "2", "]"}], "]"}], "]"}]}], ";", RowBox[{"nk", "=", RowBox[{"Length", "[", RowBox[{"a", "[", RowBox[{"[", "3", "]"}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"braInds", "=", RowBox[{"Cases", "[", RowBox[{ RowBox[{"a", "[", RowBox[{"[", "2", "]"}], "]"}], ",", RowBox[{"x_particleIndex", "\[Rule]", RowBox[{"Append", "[", RowBox[{"x", ",", RowBox[{"indexType", "[", "bra", "]"}]}], " ", "]"}]}]}], " ", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"ketInds", "=", RowBox[{"Cases", "[", RowBox[{ RowBox[{"a", "[", RowBox[{"[", "3", "]"}], "]"}], ",", RowBox[{"x_particleIndex", "\[Rule]", RowBox[{"Append", "[", RowBox[{"x", ",", RowBox[{"indexType", "[", "ket", "]"}]}], " ", "]"}]}]}], " ", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"FlattenAt", "[", RowBox[{ RowBox[{"SQM", "[", RowBox[{"FlattenAt", "[", RowBox[{ RowBox[{"{", RowBox[{"braInds", ",", "ketInds"}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"{", "1", "}"}], ",", RowBox[{"{", "2", "}"}]}], "}"}]}], "]"}], "]"}], ",", RowBox[{"{", "1", "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"Prepend", "[", RowBox[{"result", ",", RowBox[{"OHead", "[", RowBox[{ RowBox[{"a", "[", RowBox[{"[", "1", "]"}], "]"}], ",", RowBox[{"a", "[", RowBox[{"[", "4", "]"}], "]"}]}], "]"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "result", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", " ", RowBox[{ "these", " ", "functions", " ", "display", " ", "SQ", " ", "expressions", " ", "in", " ", "tensor", " ", "notation"}], " ", "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"visualizeSQE", "[", RowBox[{"a_", "*", "b_"}], "]"}], " ", ":=", " ", RowBox[{ RowBox[{"visualizeSQE", "[", "a", "]"}], "*", RowBox[{"visualizeSQE", "[", "b", "]"}]}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"visualizeSQE", "[", RowBox[{"a_", "**", "b_"}], "]"}], " ", ":=", " ", RowBox[{ RowBox[{"visualizeSQE", "[", "a", "]"}], "**", RowBox[{"visualizeSQE", "[", "b", "]"}]}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"visualizeSQE", "[", RowBox[{"a_", "^", "n_Integer"}], "]"}], " ", ":=", " ", RowBox[{ RowBox[{"visualizeSQE", "[", "a", "]"}], "^", "n"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"visualizeSQE", "[", RowBox[{"a_", "+", "b_"}], "]"}], ":=", RowBox[{ RowBox[{"visualizeSQE", "[", "a", "]"}], "+", RowBox[{"visualizeSQE", "[", "b", "]"}]}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"visualizeSQE", "[", RowBox[{"a_", "/;", RowBox[{"(", RowBox[{ RowBox[{ RowBox[{"Head", "[", "a", "]"}], "=!=", "SQS"}], "&&", RowBox[{ RowBox[{"Head", "[", "a", "]"}], "=!=", "deltaIndex"}], "&&", RowBox[{ RowBox[{"Head", "[", "a", "]"}], "=!=", "SQM"}]}], ")"}]}], "]"}], " ", ":=", " ", "a"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"visualizeSQE", "[", "a_deltaIndex", "]"}], " ", ":=", " ", RowBox[{"Subsuperscript", "[", RowBox[{"\"\<\[Delta]\>\"", ",", RowBox[{"a", "[", RowBox[{"[", RowBox[{"1", ",", "1"}], "]"}], "]"}], ",", RowBox[{"a", "[", RowBox[{"[", RowBox[{"2", ",", "1"}], "]"}], "]"}]}], " ", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"visualizeSQE", "[", "a_SQS", "]"}], " ", ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{ "bodyLabel", ",", "nsup", ",", "nsub", ",", "inds", ",", "i"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"bodyLabel", "=", "\"\<\[ATilde]\>\""}], ";", RowBox[{"supInds", "=", "\"\<\>\""}], ";", RowBox[{"subInds", "=", "\"\<\>\""}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{ RowBox[{"Cases", "[", RowBox[{ RowBox[{"a", "[", RowBox[{"[", "i", "]"}], "]"}], ",", "_indexType"}], "]"}], "[", RowBox[{"[", RowBox[{"1", ",", "1"}], "]"}], "]"}], "===", "cre"}], ",", "\[IndentingNewLine]", RowBox[{"supInds", "=", RowBox[{"StringJoin", "[", RowBox[{"supInds", ",", RowBox[{"a", "[", RowBox[{"[", RowBox[{"i", ",", "1"}], "]"}], "]"}]}], " ", "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"subInds", "=", RowBox[{"StringJoin", "[", RowBox[{ RowBox[{"a", "[", RowBox[{"[", RowBox[{"i", ",", "1"}], "]"}], "]"}], ",", "subInds"}], " ", "]"}]}]}], "\[IndentingNewLine]", "]"}], ",", RowBox[{"{", RowBox[{"i", ",", "1", ",", RowBox[{"Length", "[", "a", "]"}]}], " ", "}"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", RowBox[{"Subsuperscript", "[", RowBox[{"bodyLabel", ",", "subInds", ",", "supInds"}], "]"}], "]"}]}]}], " ", "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"visualizeSQE", "[", "a_SQM", "]"}], " ", ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{ "bodyLabel", ",", "nsup", ",", "nsub", ",", "inds", ",", "i"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"bodyLabel", "=", RowBox[{"a", "[", RowBox[{"[", RowBox[{"1", ",", "1"}], "]"}], "]"}]}], ";", RowBox[{"supInds", "=", "\"\<\>\""}], ";", RowBox[{"subInds", "=", "\"\<\>\""}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{"indexKetQ", "[", RowBox[{"a", "[", RowBox[{"[", "i", "]"}], "]"}], "]"}], ",", "\[IndentingNewLine]", RowBox[{"supInds", "=", RowBox[{"StringJoin", "[", RowBox[{"supInds", ",", RowBox[{"a", "[", RowBox[{"[", RowBox[{"i", ",", "1"}], "]"}], "]"}]}], " ", "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"subInds", "=", RowBox[{"StringJoin", "[", RowBox[{"subInds", ",", RowBox[{"a", "[", RowBox[{"[", RowBox[{"i", ",", "1"}], "]"}], "]"}]}], " ", "]"}]}]}], "\[IndentingNewLine]", "]"}], ",", RowBox[{"{", RowBox[{"i", ",", "2", ",", RowBox[{"Length", "[", "a", "]"}]}], " ", "}"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", RowBox[{"Subsuperscript", "[", RowBox[{"bodyLabel", ",", "subInds", ",", "supInds"}], "]"}], "]"}]}]}], " ", "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"Format", "[", RowBox[{ RowBox[{"deltaIndex", "[", "a__", "]"}], ",", "TraditionalForm"}], "]"}], ":=", RowBox[{"visualizeSQE", "[", RowBox[{"deltaIndex", "[", "a", "]"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"Format", "[", RowBox[{ RowBox[{"SQM", "[", "a__", "]"}], ",", "TraditionalForm"}], "]"}], ":=", RowBox[{"visualizeSQE", "[", RowBox[{"SQM", "[", "a", "]"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"Format", "[", RowBox[{ RowBox[{"SQS", "[", "a__", "]"}], ",", "TraditionalForm"}], "]"}], ":=", RowBox[{"visualizeSQE", "[", RowBox[{"SQS", "[", "a", "]"}], "]"}]}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", "\[IndentingNewLine]", RowBox[{"Low", "-", RowBox[{ "level", " ", "contraction", " ", "routines", "\[IndentingNewLine]", "\[IndentingNewLine]", "contractIndex", " ", "contracts", " ", "2", " ", "indices", "\[IndentingNewLine]", "contractSQS", " ", "contracts", " ", "a", " ", "List", " ", "of", " ", "SQ", " ", "strings", " ", "of", " ", "operators", "\[IndentingNewLine]", "\[IndentingNewLine]", "deltaIndex", " ", "is", " ", "our", " ", "representation", " ", "of", " ", "Kroneker", " ", "delta"}]}], "\[IndentingNewLine]", "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"contractIndex", "[", RowBox[{"L_particleIndex", ",", "R_particleIndex"}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{ "iL", ",", "iR", ",", "spaceL", ",", "spaceR", ",", "typeL", ",", "typeR", ",", " ", "fermiL", ",", " ", "fermiR"}], "}"}], ",", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", " ", RowBox[{ "check", " ", "if", " ", "particles", " ", "are", " ", "the", " ", "same"}], " ", "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{ RowBox[{"typeL", "=", RowBox[{"Cases", "[", RowBox[{ RowBox[{"L", "[", RowBox[{"[", "2", "]"}], "]"}], ",", "_particleType"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"typeR", "=", RowBox[{"Cases", "[", RowBox[{ RowBox[{"R", "[", RowBox[{"[", "2", "]"}], "]"}], ",", "_particleType"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"typeL", "=!=", "typeR"}], ",", RowBox[{"Return", "[", "0", "]"}]}], "]"}], ";", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", RowBox[{ RowBox[{ "if", " ", "both", " ", "creation", " ", "or", " ", "both", " ", "annihilation"}], " ", "-", " ", RowBox[{"result", " ", "is", " ", "0"}]}], "*)"}], "Commentary"], StyleBox["\[IndentingNewLine]", "Commentary"], RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"L", "[", RowBox[{"[", "3", "]"}], "]"}], "\[Equal]", RowBox[{"R", "[", RowBox[{"[", "3", "]"}], "]"}]}], ",", RowBox[{"Return", "[", "0", "]"}]}], "]"}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"fermiL", "=", RowBox[{"spaceWRTFermiLevel", "[", RowBox[{"L", "[", RowBox[{"[", "2", "]"}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{ RowBox[{"L", "[", RowBox[{"[", "3", "]"}], "]"}], "\[Equal]", RowBox[{"indexType", "[", "ann", "]"}]}], "&&", RowBox[{"fermiL", "\[Equal]", RowBox[{"-", "1"}]}]}], ",", RowBox[{"Return", "[", "0", "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{ RowBox[{"L", "[", RowBox[{"[", "3", "]"}], "]"}], "\[Equal]", RowBox[{"indexType", "[", "cre", "]"}]}], "&&", RowBox[{"fermiL", "\[Equal]", RowBox[{"+", "1"}]}]}], ",", RowBox[{"Return", "[", "0", "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"fermiR", "=", RowBox[{"spaceWRTFermiLevel", "[", RowBox[{"R", "[", RowBox[{"[", "2", "]"}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{ RowBox[{"R", "[", RowBox[{"[", "3", "]"}], "]"}], "\[Equal]", RowBox[{"indexType", "[", "cre", "]"}]}], "&&", RowBox[{"fermiR", "\[Equal]", RowBox[{"-", "1"}]}]}], ",", RowBox[{"Return", "[", "0", "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{ RowBox[{"R", "[", RowBox[{"[", "3", "]"}], "]"}], "\[Equal]", RowBox[{"indexType", "[", "ann", "]"}]}], "&&", RowBox[{"fermiR", "\[Equal]", RowBox[{"+", "1"}]}]}], ",", RowBox[{"Return", "[", "0", "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"!", RowBox[{"spacesOverlap", "[", RowBox[{ RowBox[{"L", "[", RowBox[{"[", "2", "]"}], "]"}], ",", RowBox[{"R", "[", RowBox[{"[", "2", "]"}], "]"}]}], "]"}]}], ",", RowBox[{"Return", "[", "0", "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "5"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"L", "//", "TraditionalForm"}], ",", "\"\< and \>\"", ",", " ", RowBox[{"R", "//", "TraditionalForm"}]}], "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"L", "[", RowBox[{"[", "1", "]"}], "]"}], "\[Equal]", RowBox[{"R", "[", RowBox[{"[", "1", "]"}], "]"}]}], ",", RowBox[{"Return", "[", "1", "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"R", "[", RowBox[{"[", "3", "]"}], "]"}], "===", RowBox[{"indexType", "[", "cre", "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"Return", "[", RowBox[{"deltaIndex", "[", RowBox[{ RowBox[{"Drop", "[", RowBox[{"L", ",", RowBox[{"{", "3", "}"}]}], "]"}], ",", RowBox[{"Drop", "[", RowBox[{"R", ",", RowBox[{"{", "3", "}"}]}], "]"}]}], " ", "]"}], " ", "]"}], ",", "\[IndentingNewLine]", RowBox[{"Return", "[", RowBox[{"deltaIndex", "[", RowBox[{ RowBox[{"Drop", "[", RowBox[{"R", ",", RowBox[{"{", "3", "}"}]}], "]"}], ",", RowBox[{"Drop", "[", RowBox[{"L", ",", RowBox[{"{", "3", "}"}]}], "]"}]}], " ", "]"}], " ", "]"}]}], " ", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"deltaQ", "[", RowBox[{"a_", ",", "b_deltaIndex"}], "]"}], ":=", RowBox[{"MemberQ", "[", RowBox[{"a", ",", RowBox[{"c_deltaIndex", "/;", RowBox[{"(", RowBox[{ RowBox[{"indexQ", "[", RowBox[{"c", ",", RowBox[{"b", "[", RowBox[{"[", "1", "]"}], "]"}]}], " ", "]"}], "&&", RowBox[{"indexQ", "[", RowBox[{"c", ",", RowBox[{"b", "[", RowBox[{"[", "2", "]"}], "]"}]}], " ", "]"}]}], ")"}]}]}], "]"}]}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", "\[IndentingNewLine]", RowBox[{ "CR", " ", "is", " ", "the", " ", "head", " ", "for", " ", "a", " ", "general", " ", "result", " ", "of", " ", "a", " ", "contraction", " ", RowBox[{"(", RowBox[{"unless", " ", RowBox[{"it", "'"}], "s", " ", "a", " ", "zero"}], ")"}], "\[IndentingNewLine]", "such", " ", "a", " ", "result", " ", "consists", " ", "of", " ", "a", " ", "prefactor", " ", "and", " ", "a", " ", "noncommutative", " ", "product", " ", "of", " ", "SQ", " ", RowBox[{"strings", ":", "\[IndentingNewLine]", RowBox[{"CR", "[", RowBox[{"pfac", ",", "body"}], "]"}]}]}], "\[IndentingNewLine]", "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"factorIntoCR", "[", RowBox[{"a_", ",", "0"}], "]"}], ":=", "0"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"factorIntoCR", "[", RowBox[{"a_", ",", "b_CR"}], "]"}], ":=", RowBox[{"CR", "[", RowBox[{ RowBox[{"a", "*", RowBox[{"b", "[", RowBox[{"[", "1", "]"}], "]"}]}], ",", RowBox[{"b", "[", RowBox[{"[", "2", "]"}], "]"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"factorIntoCR", "[", RowBox[{"a_", ",", RowBox[{"b", ":", RowBox[{"Plus", "[", "__", "]"}]}]}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"result", ",", "nterms"}], "}"}], ",", RowBox[{ RowBox[{"result", "=", "0"}], ";", RowBox[{"nterms", "=", RowBox[{"Length", "[", "b", "]"}]}], ";", RowBox[{"Do", "[", RowBox[{ RowBox[{"result", "+=", RowBox[{"factorIntoCR", "[", RowBox[{"a", ",", RowBox[{"b", "[", RowBox[{"[", "i", "]"}], "]"}]}], "]"}]}], ",", RowBox[{"{", RowBox[{"i", ",", "1", ",", "nterms"}], "}"}]}], "]"}], ";", RowBox[{"Return", "[", "result", "]"}], ";"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"contractSQS", "[", RowBox[{ RowBox[{"str", ":", RowBox[{"NCM", "[", "__SQS", "]"}]}], ",", "ptype_particleType", ",", "contractOptions_List"}], "]"}], " ", ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{ "result", ",", "sqsL", ",", "sqsR", ",", "indexL", ",", "indexR", ",", "indL", ",", "indR", ",", "iR", ",", "indRoff", ",", "contr1", ",", "contr2", ",", "newstr"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"Depth", "[", "str", "]"}], "\[Equal]", "2"}], ",", RowBox[{"Return", "[", RowBox[{"CR", "[", RowBox[{"1", ",", "1"}], "]"}], "]"}]}], "]"}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", RowBox[{"Recursively", " ", "drop", " ", "empty", " ", "strings"}], "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"{", "}"}]}], ";", StyleBox["\[IndentingNewLine]", "Commentary"], RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"sqsL", "=", "sL"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"Length", "[", RowBox[{"str", "[", RowBox[{"[", "sL", "]"}], "]"}], "]"}], "\[Equal]", "0"}], ",", RowBox[{ RowBox[{"result", "=", RowBox[{"contractSQS", "[", RowBox[{ RowBox[{"Drop", "[", RowBox[{"str", ",", RowBox[{"{", "sL", "}"}]}], "]"}], ",", "ptype", ",", "contractOptions"}], " ", "]"}]}], ";", RowBox[{"Break", "[", "]"}]}]}], "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"sL", ",", "1", ",", RowBox[{"Length", "[", "str", "]"}]}], "}"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"result", "=!=", RowBox[{"{", "}"}]}], ",", RowBox[{"Return", "[", "result", "]"}]}], "]"}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", RowBox[{"Cannot", " ", "contract", " ", "single", " ", "string"}], "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"Length", "[", "str", "]"}], "\[Equal]", "1"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"result", "=", RowBox[{"If", "[", RowBox[{ RowBox[{"!", RowBox[{"(", RowBox[{"fullContract", "/.", "contractOptions"}], ")"}]}], ",", RowBox[{"CR", "[", RowBox[{"1", ",", RowBox[{"str", "[", RowBox[{"[", "1", "]"}], "]"}]}], "]"}], ",", "0"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "result", "]"}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", RowBox[{ "Find", " ", "the", " ", "first", " ", "string", " ", "that", " ", "has", " ", "an", " ", "index", " ", "of", " ", "ptype"}], "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{"indRoff", "=", "0"}], ";", "\[IndentingNewLine]", RowBox[{"sqsL", "=", RowBox[{"-", "1"}]}], ";", StyleBox["\[IndentingNewLine]", "Commentary"], RowBox[{"Do", "[", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"indL", "=", RowBox[{"firstIndexOfType", "[", RowBox[{ RowBox[{"str", "[", RowBox[{"[", "sL", "]"}], "]"}], ",", "ptype"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"indRoff", "+=", RowBox[{"numIndicesOfType", "[", RowBox[{ RowBox[{"str", "[", RowBox[{"[", "sL", "]"}], "]"}], ",", "ptype"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"indL", "\[NotEqual]", RowBox[{"-", "1"}]}], ",", RowBox[{ RowBox[{"sqsL", "=", "sL"}], ";", RowBox[{"Break", "[", "]"}]}]}], "]"}]}], ",", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"sL", ",", "1", ",", RowBox[{ RowBox[{"Length", "[", "str", "]"}], "-", "1"}]}], "}"}]}], "]"}], ";", StyleBox["\[IndentingNewLine]", "Commentary"], StyleBox[ RowBox[{"(*", RowBox[{ "exit", " ", "if", " ", "such", " ", "string", " ", "is", " ", "not", " ", "found"}], "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"sqsL", "\[Equal]", RowBox[{"-", "1"}]}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"result", "=", RowBox[{"If", "[", RowBox[{ RowBox[{"(", RowBox[{"fullContract", "/.", "contractOptions"}], ")"}], ",", "0", ",", RowBox[{"CR", "[", RowBox[{"1", ",", "str"}], "]"}]}], " ", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "result", "]"}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"indexL", "=", RowBox[{"str", "[", RowBox[{"[", RowBox[{"sqsL", ",", "indL"}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", RowBox[{ "Contract", " ", "with", " ", "every", " ", "other", " ", "index", " ", "of", " ", "ptype"}], "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{"result", "=", "0"}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"iR", "=", "0"}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", " ", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"indexR", " ", "=", RowBox[{"str", "[", RowBox[{"[", RowBox[{"sqsR", ",", "indR"}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"indexParticle", "[", "indexR", "]"}], "=!=", "ptype"}], ",", RowBox[{"Continue", "[", "]"}], ",", RowBox[{"iR", "+=", "1"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "5"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"indexL", "//", "TraditionalForm"}], ",", "\"\< and \>\"", ",", " ", RowBox[{"indexR", "//", "TraditionalForm"}]}], "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"contr1", "=", RowBox[{"contractIndex", "[", RowBox[{"indexL", ",", "indexR"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "5"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"contr1", "//", "TraditionalForm"}]}], "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"contr1", "=!=", "0"}], ",", RowBox[{"(", RowBox[{ RowBox[{"newstr", "=", RowBox[{"Join", "[", RowBox[{ RowBox[{"Take", "[", RowBox[{"str", ",", RowBox[{"{", RowBox[{"1", ",", RowBox[{"sqsL", "-", "1"}]}], "}"}]}], "]"}], ",", RowBox[{"NCM", "[", RowBox[{"Drop", "[", RowBox[{ RowBox[{"str", "[", RowBox[{"[", "sqsL", "]"}], "]"}], ",", RowBox[{"{", "indL", "}"}]}], "]"}], "]"}], ",", RowBox[{"Take", "[", RowBox[{"str", ",", RowBox[{"{", RowBox[{ RowBox[{"sqsL", "+", "1"}], ",", RowBox[{"sqsR", "-", "1"}]}], "}"}]}], "]"}], ",", RowBox[{"NCM", "[", RowBox[{"Drop", "[", RowBox[{ RowBox[{"str", "[", RowBox[{"[", "sqsR", "]"}], "]"}], ",", RowBox[{"{", "indR", "}"}]}], "]"}], "]"}], ",", RowBox[{"Take", "[", RowBox[{"str", ",", RowBox[{"{", RowBox[{ RowBox[{"sqsR", "+", "1"}], ",", RowBox[{"Length", "[", "str", "]"}]}], "}"}]}], "]"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "5"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\\"", ",", "contr1"}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"newstr", "//", "TraditionalForm"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "sqsL", ",", "\"\< \>\"", ",", "indL", ",", "\"\< \>\"", ",", "sqsR", ",", "\"\< \>\"", ",", "indR", ",", "\"\< iR=\>\"", ",", "iR", ",", "\"\< indRoff=\>\"", ",", "indRoff"}], "]"}], ";"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"contr2", "=", RowBox[{"contractSQS", "[", RowBox[{"newstr", ",", "ptype", ",", "contractOptions"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"result", " ", "+=", RowBox[{"factorIntoCR", "[", RowBox[{ RowBox[{ RowBox[{ RowBox[{"(", RowBox[{"-", "1"}], ")"}], "^", RowBox[{"(", RowBox[{"iR", "+", "indRoff", "-", "2"}], ")"}]}], "*", "contr1"}], ",", "contr2"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "5"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"contr2", "//", "TraditionalForm"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"result", "//", "TraditionalForm"}]}], "]"}]}]}], "\[IndentingNewLine]", "]"}], ";"}], "\[IndentingNewLine]", ")"}]}], "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"indR", ",", "1", ",", RowBox[{"Length", "[", RowBox[{"str", "[", RowBox[{"[", "sqsR", "]"}], "]"}], "]"}]}], "}"}]}], "]"}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"indRoff", "+=", RowBox[{"numIndicesOfType", "[", RowBox[{ RowBox[{"str", "[", RowBox[{"[", "sqsR", "]"}], "]"}], ",", "ptype"}], "]"}]}]}], ",", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"sqsR", ",", RowBox[{"sqsL", "+", "1"}], ",", RowBox[{"Length", "[", "str", "]"}]}], "}"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "result", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"contractSQS", "[", RowBox[{ RowBox[{"NCM", "[", "]"}], ",", "_particleType", ",", "_List"}], "]"}], ":=", RowBox[{"CR", "[", RowBox[{"1", ",", "1"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"contractSQS", "[", RowBox[{"0", ",", "_particleType", ",", "_List"}], "]"}], ":=", "0"}], ";"}], "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", RowBox[{"contractSQS", " ", "is", " ", "distributive"}], "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"contractSQS", "[", RowBox[{ "str_Plus", ",", "ptype_particleType", ",", "contractOptions_List"}], "]"}], ":=", "\[IndentingNewLine]", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"result", ",", "nterms"}], "}"}], ",", RowBox[{ RowBox[{"result", "=", "0"}], ";", RowBox[{"nterms", "=", RowBox[{"Length", "[", "str", "]"}]}], ";", RowBox[{"Do", "[", RowBox[{ RowBox[{"result", "+=", RowBox[{"contractSQS", "[", RowBox[{ RowBox[{"str", "[", RowBox[{"[", "i", "]"}], "]"}], ",", "ptype", ",", "contractOptions"}], "]"}]}], ",", RowBox[{"{", RowBox[{"i", ",", "1", ",", "nterms"}], "}"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "2"}], ",", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "str", ",", "\"\< result = \>\"", ",", "result"}], "]"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "result", "]"}], ";"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", RowBox[{ "separate", " ", "out", " ", "prefactors", " ", "before", " ", "the", " ", "contraction"}], "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{"(*", RowBox[{ RowBox[{ RowBox[{"contractSQS", "[", RowBox[{ RowBox[{ RowBox[{"(", RowBox[{"a_", "/;", RowBox[{"FreeQ", "[", RowBox[{"a", ",", "SQS"}], "]"}]}], ")"}], "*", "b_"}], ",", "ptype_particleType", ",", "contractOptions_List"}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", "result", "}"}], ",", RowBox[{ RowBox[{"result", "=", RowBox[{"contractSQS", "[", RowBox[{"b", ",", "ptype", ",", "contractOptions"}], "]"}]}], ";", RowBox[{"result", "=", RowBox[{"CR", "[", RowBox[{ RowBox[{"a", "*", RowBox[{"result", "[", RowBox[{"[", "1", "]"}], "]"}]}], ",", RowBox[{"result", "[", RowBox[{"[", "2", "]"}], "]"}]}], "]"}]}], ";", RowBox[{"Return", "[", "result", "]"}], ";"}]}], "]"}]}], ";"}], "*)"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"contractSQS", "[", RowBox[{ "str_CR", ",", "ptype_particleType", ",", "contractOptions_List"}], "]"}], ":=", "\[IndentingNewLine]", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", "result", "}"}], ",", RowBox[{ RowBox[{"result", "=", RowBox[{"contractSQS", "[", RowBox[{ RowBox[{"str", "[", RowBox[{"[", "2", "]"}], "]"}], ",", "ptype", ",", "contractOptions"}], "]"}]}], ";", RowBox[{"result", "=", RowBox[{"factorIntoCR", "[", RowBox[{ RowBox[{"str", "[", RowBox[{"[", "1", "]"}], "]"}], ",", "result"}], "]"}]}], ";", RowBox[{"Return", "[", "result", "]"}], ";"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", RowBox[{ "contracting", " ", "a", " ", "single", " ", "string", " ", "is", " ", "easy"}], "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"contractSQS", "[", RowBox[{ "a_SQS", ",", "ptype_particleType", ",", "contractOptions_List"}], "]"}], ":=", RowBox[{"If", "[", RowBox[{ RowBox[{"(", RowBox[{"fullContract", "/.", "contractOptions"}], ")"}], ",", "0", ",", RowBox[{"CR", "[", RowBox[{"1", ",", "a"}], "]"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", RowBox[{ RowBox[{"This", " ", "is", " ", "a", " ", "top"}], "-", RowBox[{ "level", " ", "string", " ", "contraction", " ", "routine", " ", "that", " ", "applies", " ", "contractions", " ", "to", " ", "each", " ", "particle", " ", "type"}]}], "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"contractSQSNTypes", "[", RowBox[{ RowBox[{"str", ":", RowBox[{"NCM", "[", "__SQS", "]"}]}], ",", "contractOptions_List"}], "]"}], " ", ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{ "result", ",", "pfac", ",", "nstr", ",", "ind", ",", "ptypes", ",", "ntypes", ",", "intermedOptions"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"nstr", "=", RowBox[{"Length", "[", "str", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"ptypes", "=", RowBox[{"{", "}"}]}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{"ptypes", "=", RowBox[{"Append", "[", RowBox[{"ptypes", ",", RowBox[{"indexTypesSQS", "[", RowBox[{"str", "[", RowBox[{"[", "ind", "]"}], "]"}], "]"}]}], "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"ind", ",", "1", ",", "nstr"}], "}"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"ptypes", "=", RowBox[{"Union", "[", RowBox[{"Flatten", "[", "ptypes", "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"ntypes", "=", RowBox[{"Length", "[", "ptypes", "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", RowBox[{ "All", " ", "intermediate", " ", "contractions", " ", "are", " ", "incomplete", " ", "contractions"}], "*)"}], "Commentary"], StyleBox["\[IndentingNewLine]", "Commentary"], RowBox[{"intermedOptions", "=", RowBox[{"Cases", "[", RowBox[{"contractOptions", ",", RowBox[{"a_", "/;", RowBox[{"FreeQ", "[", RowBox[{"a", ",", "fullContract"}], "]"}]}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"intermedOptions", "=", RowBox[{"Append", "[", RowBox[{"intermedOptions", ",", RowBox[{"fullContract", "\[Rule]", "False"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", "str"}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"result", "=", RowBox[{"contractSQS", "[", RowBox[{"result", ",", RowBox[{"ptypes", "[", RowBox[{"[", "ind", "]"}], "]"}], ",", "intermedOptions"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "4"}], ",", RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "ind", ",", "\"\< contractions is: \>\"", ",", RowBox[{"result", "//", "TraditionalForm"}]}], "]"}]}], "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"ind", ",", "1", ",", RowBox[{"ntypes", "-", "1"}]}], "}"}]}], "]"}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", RowBox[{ "The", " ", "last", " ", "contraction", " ", "carried", " ", "according", " ", "to", " ", RowBox[{"user", "'"}], "s", " ", "specs"}], "*)"}], "Commentary"], StyleBox["\[IndentingNewLine]", "Commentary"], RowBox[{"result", "=", RowBox[{"contractSQS", "[", RowBox[{"result", ",", RowBox[{"ptypes", "[", RowBox[{"[", "ntypes", "]"}], "]"}], ",", "contractOptions"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "4"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"result", "//", "TraditionalForm"}]}], "]"}]}], "]"}], ";", StyleBox["\[IndentingNewLine]", "Commentary"], StyleBox[ RowBox[{"(*", RowBox[{"Remove", " ", "CR", " ", "heads"}], "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"Replace", "[", RowBox[{"result", ",", RowBox[{"CR", "\[Rule]", "Times"}], ",", RowBox[{"-", "1"}], ",", RowBox[{"Heads", "\[Rule]", "True"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "4"}], ",", RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", RowBox[{"result", "//", "TraditionalForm"}]}], "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "result", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"contractSQSNTypes", "[", RowBox[{ RowBox[{"{", "}"}], ",", "_particleType", ",", "_List"}], "]"}], ":=", "1"}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", "\[IndentingNewLine]", RowBox[{"wick", " ", "applies", " ", RowBox[{"Wick", "'"}], "s", " ", "theorem", " ", "to", " ", "arbitrary", " ", "SQ", " ", "expressions", "\[IndentingNewLine]", "\[IndentingNewLine]", "since", " ", "SQS", " ", "do", " ", "not", " ", "generally", " ", "commute", " ", "with", " ", "each", " ", "other", " ", "the", " ", "NonCommutativeMultiply", " ", RowBox[{"(*", "*)"}], " ", "has", "\[IndentingNewLine]", "to", " ", "be", " ", "used", " ", "in", " ", "such", " ", "expressions"}], "\[IndentingNewLine]", "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"wick", "::", "wrongargs"}], "=", "\"\\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Unprotect", "[", "defaultWickOptions", "]"}], ";"}], "\n", RowBox[{ RowBox[{"defaultWickOptions", "=", RowBox[{"{", "\[IndentingNewLine]", RowBox[{ RowBox[{"fullContract", "\[Rule]", "True"}], ",", "\[IndentingNewLine]", RowBox[{"noCoincidences", "\[Rule]", "False"}], ",", "\[IndentingNewLine]", RowBox[{"doSums", "\[Rule]", "True"}], ",", "\[IndentingNewLine]", RowBox[{"doReindex", "\[Rule]", "True"}]}], "\[IndentingNewLine]", "}"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Protect", "[", "defaultWickOptions", "]"}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"wick", "[", RowBox[{"expr_", ",", "extInds_List", ",", RowBox[{"wickOptions_List:", "defaultWickOptions"}]}], "]"}], ":=", "\[IndentingNewLine]", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"result", ",", "options", ",", "intinds", ",", "extinds"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"options", "=", RowBox[{"Cases", "[", RowBox[{"wickOptions", ",", RowBox[{"x_", "/;", RowBox[{"!", RowBox[{"FreeQ", "[", RowBox[{"x", ",", "fullContract"}], "]"}]}]}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"intinds", "=", RowBox[{"Sort", "[", RowBox[{"indexListOut", "[", RowBox[{"expr", ",", "extInds"}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "1"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", "\"\\"", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", "options", "]"}], ";", "\[IndentingNewLine]", RowBox[{ "Print", "[", "\"\\"", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"intinds", "//", "TraditionalForm"}], "]"}], ";"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"extinds", "=", RowBox[{"Sort", "[", "extInds", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "1"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"extinds", "//", "TraditionalForm"}]}], "]"}], ";"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"lowwick", "[", RowBox[{"expr", ",", "options"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "1"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", "\"\\"", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"result", "//", "TraditionalForm"}], "]"}], ";"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"doSums", "/.", "wickOptions"}], ",", "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"reduceWick", "[", RowBox[{"result", ",", "extinds", ",", "intinds"}], "]"}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "1"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", "\"\\"", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"result", "//", "TraditionalForm"}], "]"}]}]}], "\[IndentingNewLine]", "]"}], ";", StyleBox["\[IndentingNewLine]", "Commentary"], StyleBox[ RowBox[{"(*", " ", RowBox[{ "New", " ", "internale", " ", "indices", " ", "may", " ", "have", " ", "been", " ", "generatd", " ", "by", " ", RowBox[{"reduceWick", " ", "--"}], " ", "recompute"}], " ", "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{"intinds", "=", RowBox[{"Sort", "[", RowBox[{"indexListOut", "[", RowBox[{"result", ",", "extInds"}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "1"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{ "Print", "[", "\"\\"", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"intinds", "//", "TraditionalForm"}], "]"}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"noCoincidences", "/.", "wickOptions"}], ",", "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"result", "/.", RowBox[{"x_deltaIndex", "\[Rule]", "0"}]}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "1"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", "\"\\"", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"result", "//", "TraditionalForm"}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"intinds", "//", "TraditionalForm"}], "]"}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"doReindex", "/.", "wickOptions"}], ",", "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"reindex", "[", RowBox[{"result", ",", "intinds"}], "]"}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "1"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", "\"\\"", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"result", "//", "TraditionalForm"}], "]"}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "result", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", "\n", RowBox[{ RowBox[{ RowBox[{"lowwick", "[", RowBox[{ RowBox[{"a_", "+", "b_"}], ",", "contractOptions_List"}], "]"}], " ", ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"resulta", ",", "resultb"}], "}"}], ",", RowBox[{ RowBox[{"resulta", "=", RowBox[{"lowwick", "[", RowBox[{"a", ",", "contractOptions"}], "]"}]}], ";", RowBox[{"resultb", "=", RowBox[{"lowwick", "[", RowBox[{"b", ",", "contractOptions"}], "]"}]}], ";", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "6"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"a", "//", "TraditionalForm"}]}], "]"}]}], "]"}], ";", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "6"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"resulta", "//", "TraditionalForm"}]}], "]"}]}], "]"}], ";", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "6"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"b", "//", "TraditionalForm"}]}], "]"}]}], "]"}], ";", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "6"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"resultb", "//", "TraditionalForm"}]}], "]"}]}], "]"}], ";", RowBox[{"Return", "[", RowBox[{"resulta", "+", "resultb"}], "]"}]}]}], "]"}]}], ";"}], "\n", "\n", RowBox[{ RowBox[{ RowBox[{"lowwick", "[", RowBox[{ RowBox[{ RowBox[{"(", RowBox[{"a_", "/;", RowBox[{"FreeQ", "[", RowBox[{"a", ",", "SQS"}], "]"}]}], ")"}], "*", "b_"}], ",", "contractOptions_List"}], "]"}], ":=", RowBox[{"a", "*", RowBox[{"lowwick", "[", RowBox[{"b", ",", "contractOptions"}], "]"}]}]}], ";"}], "\n", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"lowwick", "[", RowBox[{"str_SQS", ",", "contractOptions_List"}], "]"}], " ", ":=", RowBox[{"contractSQSNTypes", "[", RowBox[{ RowBox[{"NCM", "[", "str", "]"}], ",", "contractOptions"}], "]"}]}], ";"}], "\n", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"lowwick", "[", RowBox[{ RowBox[{"(", RowBox[{"str_NCM", "/;", RowBox[{"!", RowBox[{"FreeQ", "[", RowBox[{"str", ",", "Plus"}], "]"}]}]}], ")"}], ",", "contractOptions_List"}], "]"}], " ", ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"result", ",", "dstr"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "6"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"str", "//", "TraditionalForm"}]}], "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"dstr", "=", RowBox[{"Map", "[", RowBox[{"Distribute", ",", "str", ",", RowBox[{"{", RowBox[{"0", ",", "Infinity"}], "}"}]}], "]"}]}], ";", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "6"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"dstr", "//", "TraditionalForm"}]}], "]"}]}], "]"}], ";", RowBox[{"result", "=", RowBox[{"lowwick", "[", RowBox[{"dstr", ",", "contractOptions"}], "]"}]}], ";", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "6"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"result", "//", "TraditionalForm"}]}], "]"}]}], "]"}], ";", RowBox[{"Return", "[", "result", "]"}]}]}], "]"}]}], ";"}], "\n", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"lowwick", "[", RowBox[{"str_NCM", ",", "contractOptions_List"}], "]"}], " ", ":=", "\[IndentingNewLine]", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"result", ",", "containsSQS", ",", "f", ",", "i"}], "}"}], ",", RowBox[{ RowBox[{"Clear", "[", "containsSQS", "]"}], ";", RowBox[{"Clear", "[", "f", "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "6"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"str", "//", "TraditionalForm"}]}], "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"pfac", "=", "1"}], ";", RowBox[{"result", "=", RowBox[{"NCM", "[", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"f", "[", RowBox[{ RowBox[{"(", RowBox[{"a_", "/;", RowBox[{"FreeQ", "[", RowBox[{"a", ",", "SQS"}], "]"}]}], ")"}], "*", "b_SQS"}], "]"}], " ", ":=", " ", RowBox[{"(", RowBox[{ RowBox[{"pfac", "*=", "a"}], ";", RowBox[{"result", "=", RowBox[{"Append", "[", RowBox[{"result", ",", "b"}], "]"}]}]}], ")"}]}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"f", "[", RowBox[{ RowBox[{"(", RowBox[{"a_", "/;", RowBox[{"FreeQ", "[", RowBox[{"a", ",", "SQS"}], "]"}]}], ")"}], "*", "b_NCM"}], "]"}], " ", ":=", " ", RowBox[{"(", RowBox[{ RowBox[{"pfac", "*=", "a"}], ";", RowBox[{"f", "[", "b", "]"}]}], ")"}]}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"f", "[", "b_NCM", "]"}], " ", ":=", " ", RowBox[{"(", RowBox[{"Map", "[", RowBox[{"f", ",", "b"}], "]"}], ")"}]}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"f", "[", "b_SQS", "]"}], ":=", RowBox[{"(", RowBox[{"result", "=", RowBox[{"Append", "[", RowBox[{"result", ",", "b"}], "]"}]}], ")"}]}], ";", "\[IndentingNewLine]", RowBox[{"Map", "[", RowBox[{"f", ",", "str"}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "5"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{ RowBox[{"pfac", "*", "result"}], "//", "TraditionalForm"}]}], "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"Expand", "[", RowBox[{"pfac", "*", RowBox[{"contractSQSNTypes", "[", RowBox[{"result", ",", "contractOptions"}], "]"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "5"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"result", "//", "TraditionalForm"}]}], "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "result", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", "\[IndentingNewLine]", RowBox[{ RowBox[{"Reduction", " ", "routines", "\[IndentingNewLine]", StyleBox["\[IndentingNewLine]", "Commentary"], StyleBox["reduceWick", "Commentary"], StyleBox[" ", "Commentary"], StyleBox["is", "Commentary"], StyleBox[" ", "Commentary"], StyleBox["the", "Commentary"], StyleBox[" ", "Commentary"], StyleBox["top", "Commentary"]}], "-", RowBox[{ RowBox[{ "level", " ", "routine", " ", "that", " ", "reduces", " ", "expressions"}], "\[IndentingNewLine]", "\t", RowBox[{ "produced", " ", "by", " ", "wick", " ", "by", " ", "carrying", " ", "out", " ", "summations", " ", "over", " ", "internalIndices"}], "\[IndentingNewLine]", RowBox[{"lowReduceWick", " ", "is", " ", "the", " ", "low"}]}], "-", RowBox[{"level", " ", "summation", " ", "routine"}]}], "\[IndentingNewLine]", "*)"}], "Commentary"], "\n", RowBox[{ RowBox[{"Unprotect", "[", "defaultReduceOptions", "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"defaultReduceOptions", "=", RowBox[{"{", "}"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Protect", "[", "defaultReduceOptions", "]"}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"reduceWick", "[", RowBox[{"expr_", ",", RowBox[{"externalIndices_List:", RowBox[{"{", "}"}]}], ",", RowBox[{"internalIndices_List:", RowBox[{"{", "}"}]}], ",", RowBox[{"reduceOptions_List:", "defaultReduceOptions"}]}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"result", ",", "nterms"}], "}"}], ",", RowBox[{ RowBox[{"result", "=", "0"}], ";", RowBox[{"nterms", "=", RowBox[{"Length", "[", "expr", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"Head", "[", "expr", "]"}], "===", "Plus"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"result", "=", "0"}], ";", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"GlobalIndexCounter", "=", "0"}], ";", "\[IndentingNewLine]", RowBox[{"result", "+=", RowBox[{"lowReduceWick", "[", RowBox[{ RowBox[{"expr", "[", RowBox[{"[", "i", "]"}], "]"}], ",", "externalIndices", ",", "internalIndices", ",", "reduceOptions"}], "]"}]}]}], ",", RowBox[{"{", RowBox[{"i", ",", "1", ",", "nterms"}], "}"}]}], "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"lowReduceWick", "[", RowBox[{ "expr", ",", "externalIndices", ",", "internalIndices", ",", "reduceOptions"}], "]"}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "result", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"lowReduceWick", "[", RowBox[{ "expr_", ",", "externalIndices_List", ",", "internalIndices_List", ",", "reduceOptions_List"}], "]"}], ":=", "\[IndentingNewLine]", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{ "deltaRepl", ",", "result", ",", "indI", ",", "indJ", ",", "spaceI", ",", "spaceJ", ",", "indK", ",", "extI", ",", "pairIJ"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"deltaRepl", "=", RowBox[{"{", "}"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"indI", "=", RowBox[{"internalIndices", "[", RowBox[{"[", "intI", "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"spaceI", "=", RowBox[{"indexSpace", "[", "indI", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"indJ", "=", RowBox[{"internalIndices", "[", RowBox[{"[", "intJ", "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"spaceJ", "=", RowBox[{"indexSpace", "[", "indJ", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"spaceK", "=", RowBox[{"Intersection", "[", RowBox[{"spaceI", ",", "spaceJ"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"spaceI", "===", "spaceK"}], ",", RowBox[{"indK", "=", "indI"}], ",", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"spaceJ", "===", "spaceK"}], ",", RowBox[{"indK", "=", "indJ"}], ",", "\[IndentingNewLine]", RowBox[{"indK", "=", RowBox[{"createParticleIndex", "[", RowBox[{ RowBox[{"ToString", "[", RowBox[{ RowBox[{"Subscript", "[", RowBox[{ RowBox[{"DefaultSpaceSymbol", "[", "spaceK", "]"}], ",", "\[IndentingNewLine]", RowBox[{"GlobalIndexCounter", "++"}]}], "]"}], ",", "TraditionalForm"}], "]"}], ",", "spaceK"}], "]"}]}]}], "\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"pairIJ", "=", RowBox[{"{", RowBox[{"indK", ",", "indI", ",", "indJ"}], "}"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"deltaQ", "[", RowBox[{"expr", ",", RowBox[{"deltaIndex", "[", RowBox[{"indI", ",", "indJ"}], "]"}]}], "]"}], ",", "\[IndentingNewLine]", RowBox[{"deltaRepl", "=", RowBox[{"Append", "[", RowBox[{"deltaRepl", ",", "pairIJ"}], "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"deltaQ", "[", RowBox[{"expr", ",", RowBox[{"deltaIndex", "[", RowBox[{"indJ", ",", "indI"}], "]"}]}], " ", "]"}], ",", "\[IndentingNewLine]", RowBox[{"deltaRepl", "=", RowBox[{"Append", "[", RowBox[{"deltaRepl", ",", "pairIJ"}], "]"}]}]}], "\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]", "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"intJ", ",", RowBox[{"intI", "+", "1"}], ",", RowBox[{"Length", "[", "internalIndices", "]"}]}], "}"}]}], "\[IndentingNewLine]", "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"intI", ",", "1", ",", RowBox[{"Length", "[", "internalIndices", "]"}]}], "}"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"SeQuantDebugLevel", "\[GreaterEqual]", "2"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"expr", "//", "TraditionalForm"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "deltaRepl"}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "internalIndices"}], "]"}], ";"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{"deltaQ", "[", RowBox[{"expr", ",", RowBox[{"deltaIndex", "[", RowBox[{ RowBox[{"externalIndices", "[", RowBox[{"[", "extI", "]"}], "]"}], ",", RowBox[{"internalIndices", "[", RowBox[{"[", "intJ", "]"}], "]"}]}], "]"}]}], "]"}], ",", RowBox[{"deltaRepl", "=", RowBox[{"Append", "[", RowBox[{"deltaRepl", ",", RowBox[{"{", RowBox[{ RowBox[{"externalIndices", "[", RowBox[{"[", "extI", "]"}], "]"}], ",", RowBox[{"internalIndices", "[", RowBox[{"[", "intJ", "]"}], "]"}], ",", RowBox[{"externalIndices", "[", RowBox[{"[", "extI", "]"}], "]"}]}], "}"}]}], "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"deltaQ", "[", RowBox[{"expr", ",", RowBox[{"deltaIndex", "[", RowBox[{ RowBox[{"internalIndices", "[", RowBox[{"[", "intJ", "]"}], "]"}], ",", RowBox[{"externalIndices", "[", RowBox[{"[", "extI", "]"}], "]"}]}], "]"}]}], "]"}], ",", RowBox[{"deltaRepl", "=", RowBox[{"Append", "[", RowBox[{"deltaRepl", ",", RowBox[{"{", RowBox[{ RowBox[{"externalIndices", "[", RowBox[{"[", "extI", "]"}], "]"}], ",", RowBox[{"internalIndices", "[", RowBox[{"[", "intJ", "]"}], "]"}], ",", RowBox[{"externalIndices", "[", RowBox[{"[", "extI", "]"}], "]"}]}], "}"}]}], "]"}]}]}], "\[IndentingNewLine]", "]"}]}], "]"}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"intJ", ",", "1", ",", RowBox[{"Length", "[", "internalIndices", "]"}]}], "}"}]}], "\[IndentingNewLine]", "]"}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"extI", ",", "1", ",", RowBox[{"Length", "[", "externalIndices", "]"}]}], "}"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", "expr"}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"indK", "=", RowBox[{"deltaRepl", "[", RowBox[{"[", RowBox[{"i", ",", "1"}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"indI", "=", RowBox[{"deltaRepl", "[", RowBox[{"[", RowBox[{"i", ",", "2"}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"indJ", "=", RowBox[{"deltaRepl", "[", RowBox[{"[", RowBox[{"i", ",", "3"}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"result", "/.", RowBox[{ RowBox[{ RowBox[{"deltaIndex", "[", RowBox[{"a_", ",", "b_"}], "]"}], "/;", RowBox[{ RowBox[{"indexEquiv", "[", RowBox[{"indI", ",", "a"}], "]"}], "&&", RowBox[{"indexEquiv", "[", RowBox[{"indJ", ",", "b"}], "]"}]}]}], " ", "\[Rule]", "1"}]}]}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"result", "/.", RowBox[{ RowBox[{ RowBox[{"deltaIndex", "[", RowBox[{"b_", ",", "a_"}], "]"}], "/;", RowBox[{ RowBox[{"indexEquiv", "[", RowBox[{"indI", ",", "a"}], "]"}], "&&", RowBox[{"indexEquiv", "[", RowBox[{"indJ", ",", "b"}], "]"}]}]}], " ", "\[Rule]", "1"}]}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"(*", RowBox[{ RowBox[{"result", "=", RowBox[{"result", "/.", RowBox[{ RowBox[{"deltaIndex", "[", RowBox[{"indJ", ",", "indI"}], "]"}], "\[Rule]", "1"}]}]}], ";"}], "*)"}], "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"result", "/.", RowBox[{"indII_particleIndex", ":>", RowBox[{ RowBox[{"particleIndex", "[", RowBox[{ RowBox[{"indK", "[", RowBox[{"[", "1", "]"}], "]"}], ",", RowBox[{"indK", "[", RowBox[{"[", "2", "]"}], "]"}], ",", RowBox[{"indII", "[", RowBox[{"[", "3", "]"}], "]"}]}], "]"}], "/;", RowBox[{"(", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"indII", "[", RowBox[{"[", "1", "]"}], "]"}], "\[Equal]", RowBox[{"indI", "[", RowBox[{"[", "1", "]"}], "]"}]}], ")"}], "&&", RowBox[{"(", RowBox[{ RowBox[{"indII", "[", RowBox[{"[", "2", "]"}], "]"}], "\[Equal]", RowBox[{"indI", "[", RowBox[{"[", "2", "]"}], "]"}]}], ")"}]}], ")"}]}]}]}]}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"result", "/.", RowBox[{"indII_particleIndex", ":>", RowBox[{ RowBox[{"particleIndex", "[", RowBox[{ RowBox[{"indK", "[", RowBox[{"[", "1", "]"}], "]"}], ",", RowBox[{"indK", "[", RowBox[{"[", "2", "]"}], "]"}], ",", RowBox[{"indII", "[", RowBox[{"[", "3", "]"}], "]"}]}], "]"}], "/;", RowBox[{"(", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"indII", "[", RowBox[{"[", "1", "]"}], "]"}], "\[Equal]", RowBox[{"indJ", "[", RowBox[{"[", "1", "]"}], "]"}]}], ")"}], "&&", RowBox[{"(", RowBox[{ RowBox[{"indII", "[", RowBox[{"[", "2", "]"}], "]"}], "\[Equal]", RowBox[{"indJ", "[", RowBox[{"[", "2", "]"}], "]"}]}], ")"}]}], ")"}]}]}]}]}], ";"}], "\[IndentingNewLine]", ",", RowBox[{"{", RowBox[{"i", ",", "1", ",", RowBox[{"Length", "[", "deltaRepl", "]"}]}], "}"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "result", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", "\[IndentingNewLine]", RowBox[{ "Transformation", " ", "routines", "\[IndentingNewLine]", "\[IndentingNewLine]", "orderedForm", " ", "puts", " ", "elements", " ", "of", " ", "SQ", " ", "expressions", " ", RowBox[{"(", RowBox[{ "SQ", " ", "strings", " ", "SQS", " ", "and", " ", "matrix", " ", "elements", " ", "SQM"}], ")"}], " ", "into", " ", "their", " ", RowBox[{"\"\<\>", "canonical", "\"\<\>"}], " ", RowBox[{"form", ".", " ", "The"}], " ", "canonical", " ", "order", " ", "of", " ", "indices", " ", "of", " ", "each", " ", "type", " ", RowBox[{"(", RowBox[{ "that", " ", "belong", " ", "to", " ", "the", " ", "same", " ", "space"}], ")"}], " ", "is", " ", RowBox[{"alphabetical", ".", " ", "This"}], " ", "is", " ", "where", " ", "the", " ", "permutational", " ", "symmetry", " ", "of", " ", RowBox[{"SQM", "'"}], "s", " ", RowBox[{"matters", "."}]}], "\[IndentingNewLine]", "*)"}], "Commentary"], "\n", RowBox[{ RowBox[{ RowBox[{"orderedForm", "[", RowBox[{ RowBox[{"a_", "*", "b_"}], ",", RowBox[{"intInds_List:", RowBox[{"{", "}"}]}]}], "]"}], ":=", RowBox[{ RowBox[{"orderedForm", "[", RowBox[{"a", ",", "intInds"}], "]"}], "*", RowBox[{"orderedForm", "[", RowBox[{"b", ",", "intInds"}], "]"}]}]}], ";"}], "\n", RowBox[{ RowBox[{ RowBox[{"orderedForm", "[", RowBox[{ RowBox[{"a_", "+", "b_"}], ",", RowBox[{"intInds_List:", RowBox[{"{", "}"}]}]}], "]"}], ":=", RowBox[{ RowBox[{"orderedForm", "[", RowBox[{"a", ",", "intInds"}], "]"}], "+", RowBox[{"orderedForm", "[", RowBox[{"b", ",", "intInds"}], "]"}]}]}], ";"}], "\n", RowBox[{ RowBox[{ RowBox[{"orderedForm", "[", RowBox[{ RowBox[{"a_", "^", "n_"}], ",", RowBox[{"intInds_List:", RowBox[{"{", "}"}]}]}], "]"}], ":=", RowBox[{ RowBox[{"orderedForm", "[", RowBox[{"a", ",", "intInds"}], "]"}], "^", "n"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"orderedForm", "[", RowBox[{ RowBox[{"a_", "?", "NumberQ"}], ",", RowBox[{"intInds_List:", RowBox[{"{", "}"}]}]}], "]"}], ":=", "a"}], ";"}], "\n", RowBox[{ RowBox[{ RowBox[{"orderedForm", "[", RowBox[{"x_deltaIndex", ",", RowBox[{"intInds_List:", RowBox[{"{", "}"}]}]}], "]"}], "=", "x"}], ";"}], "\n", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"orderedForm", "[", RowBox[{"str_SQS", ",", RowBox[{"intInds_List:", RowBox[{"{", "}"}]}]}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"result", ",", "permfac", ",", "creInds", ",", "annInds"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"permfac", "=", "1"}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"compareInds", "[", RowBox[{"a_", ",", "b_"}], "]"}], ":=", RowBox[{"Order", "[", RowBox[{ RowBox[{"a", "[", RowBox[{"[", "1", "]"}], "]"}], ",", RowBox[{"b", "[", RowBox[{"[", "1", "]"}], "]"}]}], "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"creInds", "=", RowBox[{"Cases", "[", RowBox[{"str", ",", RowBox[{"a_particleIndex", "/;", RowBox[{ RowBox[{"a", "[", RowBox[{"[", "3", "]"}], "]"}], "\[Equal]", RowBox[{"indexType", "[", "cre", "]"}]}]}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"permfac", "*=", RowBox[{"Signature", "[", "creInds", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"creInds", "=", RowBox[{"Sort", "[", "creInds", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"annInds", "=", RowBox[{"Cases", "[", RowBox[{"str", ",", RowBox[{"a_particleIndex", "/;", RowBox[{ RowBox[{"a", "[", RowBox[{"[", "3", "]"}], "]"}], "\[Equal]", RowBox[{"indexType", "[", "ann", "]"}]}]}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"annInds", "=", RowBox[{"Reverse", "[", "annInds", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"permfac", "*=", RowBox[{"Signature", "[", "annInds", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"annInds", "=", RowBox[{"Sort", "[", "annInds", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"permfac", "*", RowBox[{"FlattenAt", "[", RowBox[{ RowBox[{"SQS", "[", RowBox[{"FlattenAt", "[", RowBox[{ RowBox[{"{", RowBox[{"creInds", ",", RowBox[{"Reverse", "[", "annInds", "]"}]}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"{", "1", "}"}], ",", RowBox[{"{", "2", "}"}]}], "}"}]}], "]"}], "]"}], ",", RowBox[{"{", "1", "}"}]}], "]"}]}]}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "result", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", "\n", RowBox[{ RowBox[{ RowBox[{"orderedForm", "[", RowBox[{"oper_SQM", ",", RowBox[{"intInds_List:", RowBox[{"{", "}"}]}]}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{ "result", ",", "symfac", ",", "permfac", ",", "operHead", ",", "braInds", ",", "ketInds"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"symfac", "=", RowBox[{ RowBox[{"Cases", "[", RowBox[{"oper", ",", "_indexSymm", ",", "Infinity"}], "]"}], "[", RowBox[{"[", RowBox[{"1", ",", "1"}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"symfac", "\[Equal]", "0"}], ",", RowBox[{"Return", "[", "oper", "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"permfac", "=", "1"}], ";", "\[IndentingNewLine]", RowBox[{"operHead", "=", RowBox[{ RowBox[{"Cases", "[", RowBox[{"oper", ",", "_OHead"}], "]"}], "[", RowBox[{"[", "1", "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"compareInds", "[", RowBox[{"a_", ",", "b_"}], "]"}], ":=", RowBox[{"Order", "[", RowBox[{ RowBox[{"a", "[", RowBox[{"[", "1", "]"}], "]"}], ",", RowBox[{"b", "[", RowBox[{"[", "1", "]"}], "]"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", "oper"}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", " ", "\[IndentingNewLine]", RowBox[{ RowBox[{ "canonical", " ", "order", " ", "prioritizes", " ", "external", " ", "indices", " ", "over", " ", "internal", "\[IndentingNewLine]", "to", " ", "achieve", " ", "this", " ", "tag", " ", "all", " ", "internal", " ", "indices", " ", "such", " ", "that", " ", "their", " ", "Length", " ", "is", " ", "greater"}], ",", " ", RowBox[{ "then", " ", "external", " ", "indices", " ", "will", " ", "come", " ", "first"}]}], "\[IndentingNewLine]", "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"tagIndices", "[", RowBox[{"oper", ",", "intInds"}], "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"braInds", "=", RowBox[{"Cases", "[", RowBox[{"result", ",", RowBox[{"a_particleIndex", "/;", RowBox[{ RowBox[{"a", "[", RowBox[{"[", "3", "]"}], "]"}], "\[Equal]", RowBox[{"indexType", "[", "bra", "]"}]}]}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"symfac", "\[Equal]", RowBox[{"-", "1"}]}], ",", RowBox[{"permfac", "*=", RowBox[{"Signature", "[", "braInds", "]"}]}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"braInds", "=", RowBox[{"Sort", "[", "braInds", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"ketInds", "=", RowBox[{"Cases", "[", RowBox[{"result", ",", RowBox[{"a_particleIndex", "/;", RowBox[{ RowBox[{"a", "[", RowBox[{"[", "3", "]"}], "]"}], "\[Equal]", RowBox[{"indexType", "[", "ket", "]"}]}]}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"symfac", "\[Equal]", RowBox[{"-", "1"}]}], ",", RowBox[{"permfac", "*=", RowBox[{"Signature", "[", "ketInds", "]"}]}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"ketInds", "=", RowBox[{"Sort", "[", "ketInds", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"FlattenAt", "[", RowBox[{ RowBox[{"SQM", "[", RowBox[{"FlattenAt", "[", RowBox[{ RowBox[{"{", RowBox[{"braInds", ",", "ketInds"}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"{", "1", "}"}], ",", RowBox[{"{", "2", "}"}]}], "}"}]}], "]"}], "]"}], ",", RowBox[{"{", "1", "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"Prepend", "[", RowBox[{"result", ",", "operHead"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"result", "*=", "permfac"}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"result", "=", RowBox[{"untagIndices", "[", RowBox[{"result", ",", "intInds"}], "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"Return", "[", "result", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", " ", RowBox[{"low", "-", RowBox[{ "level", " ", "function", " ", "that", " ", "produces", " ", "the", " ", "list", " ", "of", " ", "unique", " ", "indices", " ", "from", " ", "expr", " ", "which", " ", "are", " ", "present", " ", "in", " ", "intInds"}]}], " ", "*)"}], "Commentary"], "\n", RowBox[{ RowBox[{ RowBox[{"indexListIn", "[", RowBox[{"expr_", ",", "intInds_List"}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", "intIndsIn", "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"intIndsIn", "=", RowBox[{"{", "}"}]}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"indexQ", "[", RowBox[{"expr", ",", RowBox[{"intInds", "[", RowBox[{"[", "i", "]"}], "]"}]}], " ", "]"}], "&&", RowBox[{"!", RowBox[{"indexQ", "[", RowBox[{"intIndsIn", ",", RowBox[{"intInds", "[", RowBox[{"[", "i", "]"}], "]"}]}], "]"}]}]}], ",", "\[IndentingNewLine]", RowBox[{"intIndsIn", "=", RowBox[{"Append", "[", RowBox[{"intIndsIn", ",", RowBox[{"intInds", "[", RowBox[{"[", "i", "]"}], "]"}]}], "]"}]}]}], "\[IndentingNewLine]", "]"}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"i", ",", "1", ",", RowBox[{"Length", "[", "intInds", "]"}]}], "}"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "intIndsIn", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\n", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", " ", RowBox[{"low", "-", RowBox[{ "level", " ", "function", " ", "that", " ", "produces", " ", "the", " ", "list", " ", "of", " ", "unique", " ", "indices", " ", "from", " ", "expr", " ", "which", " ", "are", " ", "not", " ", "present", " ", "in", " ", "intInds"}]}], " ", "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"indexListOut", "[", RowBox[{"expr_", ",", "intInds_List"}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"indsOut", ",", "indsAll", ",", "a"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"indsOut", "=", RowBox[{"{", "}"}]}], ";", "\[IndentingNewLine]", RowBox[{"indsAll", "=", RowBox[{"Cases", "[", RowBox[{"expr", ",", "x_particleIndex", ",", "Infinity"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"a", "=", RowBox[{"indsAll", "[", RowBox[{"[", "i", "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"!", RowBox[{"indexQ", "[", RowBox[{"intInds", ",", "a"}], "]"}]}], "&&", RowBox[{"!", RowBox[{"indexQ", "[", RowBox[{"indsOut", ",", "a"}], "]"}]}]}], ",", "\[IndentingNewLine]", RowBox[{"indsOut", "=", RowBox[{"Append", "[", RowBox[{"indsOut", ",", "a"}], "]"}]}]}], "\[IndentingNewLine]", "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"i", ",", "1", ",", RowBox[{"Length", "[", "indsAll", "]"}]}], "}"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", "indsOut", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\n", "\n", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", "\[IndentingNewLine]", RowBox[{ RowBox[{ "Reindexing", " ", "routines", "\[IndentingNewLine]", "\[IndentingNewLine]", "reindex", " ", "is", " ", "the", " ", "top"}], "-", RowBox[{ "level", " ", "reindexing", " ", "routine", "\[IndentingNewLine]", "lowreindex", " ", "is", " ", "the", " ", "low"}], "-", RowBox[{"level", " ", "routine"}]}], "\[IndentingNewLine]", "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"reindex", "[", RowBox[{"expr_", ",", RowBox[{"intInds_List:", RowBox[{"{", "}"}]}]}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"result", ",", "ordexpr", ",", "tmpexpr", ",", "i"}], "}"}], ",", RowBox[{ RowBox[{"result", "=", "0"}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", " ", RowBox[{"If", " ", "result", " ", "is", " ", "a", " ", RowBox[{"constant", " ", "--"}], " ", "no", " ", "need", " ", "to", " ", "reindex"}], " ", "*)"}], "Commentary"], StyleBox["\[IndentingNewLine]", "Commentary"], RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"FreeQ", "[", RowBox[{"expr", ",", "SQM"}], "]"}], "&&", RowBox[{"FreeQ", "[", RowBox[{"expr", ",", "SQS"}], "]"}]}], ",", RowBox[{"Return", "[", "expr", "]"}]}], "]"}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", " ", RowBox[{"Put", " ", RowBox[{"SQS", "'"}], "s", " ", "and", " ", RowBox[{"SQM", "'"}], "s", " ", "in", " ", "expr", " ", "into", " ", "their", " ", "canonical", " ", "form"}], " ", "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{"ordexpr", "=", RowBox[{"orderedForm", "[", RowBox[{"expr", ",", "intInds"}], "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", " ", RowBox[{"Reorder", " ", RowBox[{"SQS", "'"}], "s", " ", "and", " ", RowBox[{"SQM", "'"}], "s", "\[IndentingNewLine]", "\[IndentingNewLine]", "Times", " ", "orders", " ", "expressions", " ", "automatically", " ", "according", " ", "to", " ", "the", " ", "canonical", " ", "order", "\[IndentingNewLine]", "\[IndentingNewLine]", "The", " ", "canonical", " ", "order", " ", "that", " ", "we", " ", "want", " ", "is", " ", "determined", " ", "by", " ", "external", " ", "indices", " ", "only", " ", "so", " ", "that", "\[IndentingNewLine]", "the", " ", "subsequent", " ", "call", " ", "to", " ", "lowreindex", " ", "can", "\[IndentingNewLine]", "simplify", " ", "results", " ", "the", " ", RowBox[{"most", "."}]}], "\[IndentingNewLine]", "*)"}], "Commentary"], StyleBox["\[IndentingNewLine]", "Commentary"], StyleBox[ RowBox[{"(*", " ", RowBox[{ "Replace", " ", "Times", " ", "with", " ", "NCM", " ", "and", " ", "reorder", " ", "into", " ", RowBox[{"\"\<\>", "new", "\"\<\>"}], " ", "canonical", " ", "order"}], "*)"}], "Commentary"], "\[IndentingNewLine]", RowBox[{"ordexpr", "=", RowBox[{"ordexpr", "/.", RowBox[{"Times", "\[Rule]", "NCM"}]}]}], ";", "\[IndentingNewLine]", RowBox[{"(*", RowBox[{ RowBox[{"Print", "[", "\"\\"", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"ordexpr", "//", "TraditionalForm"}], "]"}], ";"}], "*)"}], "\[IndentingNewLine]", RowBox[{"tmpexpr", "=", "0"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"Head", "[", "ordexpr", "]"}], "===", "Plus"}], ",", "\[IndentingNewLine]", RowBox[{"Do", "[", RowBox[{ RowBox[{"tmpexpr", "+=", RowBox[{"newcanonord", "[", RowBox[{ RowBox[{"ordexpr", "[", RowBox[{"[", "i", "]"}], "]"}], ",", "intInds"}], "]"}]}], ",", RowBox[{"{", RowBox[{"i", ",", RowBox[{"Length", "[", "ordexpr", "]"}]}], "}"}]}], "]"}], ",", "\[IndentingNewLine]", RowBox[{"tmpexpr", "=", RowBox[{"newcanonord", "[", RowBox[{"ordexpr", ",", "intInds"}], "]"}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"ordexpr", "=", "tmpexpr"}], ";", "\[IndentingNewLine]", RowBox[{"(*", RowBox[{ RowBox[{"Print", "[", "\"\\"", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"ordexpr", "//", "TraditionalForm"}], "]"}], ";"}], "*)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{ "Rename", " ", "internal", " ", "indices", " ", "using", " ", "the", " ", "canonical", " ", "list", " ", "for", " ", "each", " ", "index", " ", "space", "\[IndentingNewLine]", "\[IndentingNewLine]", "Right", " ", "now", " ", RowBox[{"there", "'"}], "s", " ", "no", " ", "additional", " ", "reordering", " ", "of", " ", "internal", " ", "indices", " ", "prior", " ", "to", " ", RowBox[{"reindexing", ".", " ", "This"}], " ", "should", " ", "be", " ", "OK", "\[IndentingNewLine]", "as", " ", "long", " ", "as", " ", "no", " ", "6"}], "-", RowBox[{ "index", " ", "quantities", " ", "appear", " ", "in", " ", "the", " ", RowBox[{"expression", ".", " ", "If"}], " ", "that", " ", "happens", " ", "then", " ", "as", " ", "additional", " ", "reordering", "\[IndentingNewLine]", "of", " ", "internal", " ", "indices", " ", "needs", " ", "to", " ", "be", " ", RowBox[{"performed", ".", " ", "The"}], " ", "algorithms", " ", "for", " ", "that", " ", "is", " ", "as", " ", RowBox[{"follows", ":", "\[IndentingNewLine]", RowBox[{"1.", " ", "within", " ", "each", " ", RowBox[{"SQS", "/", "SQM"}], " ", "order", " ", "internal", " ", "indices", " ", "which", " ", "appear", " ", "in", " ", "the", " ", "expression", " ", "for", " ", "the", " ", "first", " ", "time", " ", "according", " ", "to", " ", "the", " ", "index", " ", "of", " ", "the", " ", RowBox[{"SQS", "/", "SQM"}], " ", "in", " ", "which", " ", "this", " ", "index", " ", "appears", " ", RowBox[{"next", ".", " ", "If"}], " ", "multiple", " ", "internal", " ", "indices", " ", "from", " ", "the", " ", "current", " ", RowBox[{"SQS", "/", "SQM"}], " ", "appear", " ", "in", " ", "a", " ", "nonsymmetric", " ", "SQM", " ", "later", " ", "in", " ", "the", " ", "expression"}]}]}]}], ",", " ", RowBox[{ RowBox[{ "order", " ", "is", " ", "determined", " ", "by", " ", "the", " ", "relative", " ", "position", " ", "of", " ", "the", " ", "indices", " ", "in", " ", "that", " ", RowBox[{ "SQM", ".", "\[IndentingNewLine]", "\[IndentingNewLine]", "I"}], " ", "think", " ", "what", " ", "I", " ", "do", " ", "now", " ", "is", " ", "OK", " ", "if", " ",