Skip to content

Instantly share code, notes, and snippets.

@lucainnocenti
Last active January 10, 2016 10:14
Show Gist options
  • Save lucainnocenti/70a6ead0a3a78b593d6d to your computer and use it in GitHub Desktop.
Save lucainnocenti/70a6ead0a3a78b593d6d to your computer and use it in GitHub Desktop.
base6ToBase10Conversion[n_Integer] :=
ToExpression[StringJoin["6^^", ToString@n]]
labelToIndex[label_String] := With[{
elementsConversionRules = {"H" -> 0, "V" -> 1, "D" -> 2, "A" -> 3,
"L" -> 4, "R" -> 5}
},
If[
StringLength@label == 4,
-(1 +
base6ToBase10Conversion@
FromDigits[
Characters@StringDrop[label, 1] /. elementsConversionRules]
),
1 + base6ToBase10Conversion@
FromDigits[Characters@label /. elementsConversionRules]
]
]
indexToNumber[listOfData_List, index_Integer] := If[
index < 0,
-listOfData[[-index]],
listOfData[[index]]
]
takeMinusOutside[pos_] := ReplaceAll[
Which[
pos == 1, (-e1_) ** e2_ ** e3_ :> -e1 ** e2 ** e3,
pos == 2, e1_ ** (-e2_) ** e3_ :> -e1 ** e2 ** e3,
pos == 3, e1_ ** e2_ ** (-e3_) :> -e1 ** e2 ** e3
]
]
takeMinusOutside[] :=
takeMinusOutside[1]@*takeMinusOutside[2]@*takeMinusOutside[3]
nonCommutativeProductToLetters[prod_] := If[
Head@prod === Times,
StringJoin["-", ToUpperCase@*ToString /@ List @@ (-prod)],
StringJoin[ToUpperCase@*ToString /@ List @@ prod]
]
stokesIndicesToSumOfProducts[indices_Integer] := With[{
stokesConversionRules = {
s1 -> h + v,
s2 -> d - a,
s3 -> r - l,
s4 -> h - v
},
stokesProduct = NonCommutativeMultiply @@ (
ToExpression["s" <> ToString@#] & /@ IntegerDigits@indices
)
},
takeMinusOutside[][
stokesProduct /. stokesConversionRules // Distribute
]
]
stokesIndicesToResult[listOfData_List,
indices_Integer] :=
indexToNumber[listOfData, #] & /@ labelToIndex /@ Apply[List][
nonCommutativeProductToLetters /@
stokesIndicesToSumOfProducts[indices]
] // Total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment