Hodge dual: Difference between revisions

Sintel (talk | contribs)
Computation: clarify, also replace ceil(k/2) with k-th triangular number (gives the same result)
Sintel (talk | contribs)
Computation: add python code
Line 94: Line 94:
# Let '''C''' be the ''k''-combinations of the numbers 1 through ''n'' in lexicographic order. '''C''' will have the same length as '''V''' and '''M'''.
# Let '''C''' be the ''k''-combinations of the numbers 1 through ''n'' in lexicographic order. '''C''' will have the same length as '''V''' and '''M'''.
# For each combination <math>C_i</math>, compute <math>S_i = \sum C_i - \frac{k (k+1)}{2}</math>.
# For each combination <math>C_i</math>, compute <math>S_i = \sum C_i - \frac{k (k+1)}{2}</math>.
# Multiply the ''i''-th element of '''V''' by <math>(-1)^{S_i}</math>
# Multiply the ''i''-th element of '''V''' by <math>(-1)^{S_i}</math>.
# Reverse the elements of '''V'''.
# Reverse the elements of '''V'''.


To find an unknown '''V''' from a known '''M''', first reverse '''M''' and then adjust the signs.
To find an unknown '''V''' from a known '''M''', first reverse '''M''' and then adjust the signs.
Python implementation of the above algorithm:
{{Databox| Code |
<syntaxhighlight lang="python">
import itertools
import math
def hodge_dual(n, k, v):
    N = math.comb(n, k)
    if len(v) != N:
        raise ValueError(f"Length of v must be {N}")
    # Generate lex-ordered k-indices (tuples)
    indices_list = list(itertools.combinations(range(1, n + 1), k))
    # k-th triangular number
    T0 = k * (k + 1) // 2
    w = [0] * N
    for i, I in enumerate(indices_list):
        total = sum(I)
        exponent = total - T0
        s = 1 if exponent % 2 == 0 else -1
        j = N - 1 - i  # Position in dual vector (reverse lex order)
        w[j] = s * v[i]
    return w
if __name__ == "__main__":
    n = 3
    k = 2
    # Output: [4, -4, 1]
    print(hodge_dual(n, k, [1, 4, 4]))
</syntaxhighlight>
}}


== Applications ==
== Applications ==