| 1 | # This file provides a function getMatrices which returns the dot products of logsignature |
| 2 | # basis elements as a matrix for each level. |
| 3 | # This allows you to find the L2 dot product of log signatures in tensor space without |
| 4 | # ever expressing them in tensor space. |
| 5 | # This might be useful if you have to calculate such a dot product very many times. |
| 6 | # The demo() function provides an example. |
| 7 | |
| 8 | # Don't use for dimension bigger than 9, because it uses the basis() function |
| 9 | |
| 10 | import numpyscip-python python numpy 2.4.6 numpy/__init__: as np |
| 11 | import numpyscip-python python numpy 2.4.6 numpy/__init__: |
| 12 | import sysscip-python python python-stdlib 3.11 sys/__init__: |
| 13 | import osscip-python python python-stdlib 3.11 os/__init__: |
| 14 | import scipy.linalg |
| 15 | from sixscip-python python iisignature 0.24 six/__init__: import print_ |
| 16 | |
| 17 | # add the parent directory, so we find our iisignature build if it was built --inplace |
| 18 | sysscip-python python python-stdlib 3.11 sys/__init__:.pathscip-python python python-stdlib 3.11 sys/path.path..appendscip-python python python-stdlib 3.11 builtins/list#append().(osscip-python python python-stdlib 3.11 os/__init__:.pathscip-python python python-stdlib 3.11 os/path..dirnamescip-python python python-stdlib 3.11 posixpath/dirname().(osscip-python python python-stdlib 3.11 os/__init__:.pathscip-python python python-stdlib 3.11 os/path..abspathscip-python python python-stdlib 3.11 posixpath/dirname().(osscip-python python python-stdlib 3.11 os/__init__:.pathscip-python python python-stdlib 3.11 os/path..dirnamescip-python python python-stdlib 3.11 posixpath/dirname().(__file__scip-python python python-stdlib 3.11 builtins/__file__#)))) |
| 19 | import iisignature |
| 20 | |
| 21 | # the following 3 functions for interpreting basis strings |
| 22 | # are copied from the tests |
| 23 | |
| 24 | |
| 25 | # text, index -> (either number or [res, res]), newIndex |
| 26 | def parseBracketedExpressionscip-python python iisignature 0.24 `examples.innerProductLogSig`/parseBracketedExpression().(textscip-python python iisignature 0.24 `examples.innerProductLogSig`/parseBracketedExpression().(text), indexscip-python python iisignature 0.24 `examples.innerProductLogSig`/parseBracketedExpression().(index)): |
| 27 | if textscip-python python iisignature 0.24 `examples.innerProductLogSig`/parseBracketedExpression().(text)[indexscip-python python iisignature 0.24 `examples.innerProductLogSig`/parseBracketedExpression().(index)] == "[": |
| 28 | left, m = parseBracketedExpressionscip-python python iisignature 0.24 `examples.innerProductLogSig`/parseBracketedExpression().(textscip-python python iisignature 0.24 `examples.innerProductLogSig`/parseBracketedExpression().(text), indexscip-python python iisignature 0.24 `examples.innerProductLogSig`/parseBracketedExpression().(index) + 1) |
| 29 | right, n = parseBracketedExpressionscip-python python iisignature 0.24 `examples.innerProductLogSig`/parseBracketedExpression().(textscip-python python iisignature 0.24 `examples.innerProductLogSig`/parseBracketedExpression().(text), m + 1) |
| 30 | return [left, right], n + 1 |
| 31 | else: |
| 32 | n = 0 |
| 33 | while ( |
| 34 | n < len(textscip-python python iisignature 0.24 `examples.innerProductLogSig`/parseBracketedExpression().(text)) |
| 35 | and textscip-python python iisignature 0.24 `examples.innerProductLogSig`/parseBracketedExpression().(text)[indexscip-python python iisignature 0.24 `examples.innerProductLogSig`/parseBracketedExpression().(index) + n] in ["1", "2", "3", "4", "5", "6", "7", "8", "9"] |
| 36 | ): # this should always just happen once if input is a bracketed expression of letters |
| 37 | n = n + 1 |
| 38 | return intscip-python python python-stdlib 3.11 builtins/int#(textscip-python python iisignature 0.24 `examples.innerProductLogSig`/parseBracketedExpression().(text)[indexscip-python python iisignature 0.24 `examples.innerProductLogSig`/parseBracketedExpression().(index) : indexscip-python python iisignature 0.24 `examples.innerProductLogSig`/parseBracketedExpression().(index) + n]), indexscip-python python iisignature 0.24 `examples.innerProductLogSig`/parseBracketedExpression().(index) + n |
| 39 | |
| 40 | |
| 41 | # print (parseBracketedExpression("[23,[2,[[22,1],2]]]",0)) |
| 42 | |
| 43 | |
| 44 | # bracketed expression, dim -> numpy array of its value, depth |
| 45 | def multiplyOutscip-python python iisignature 0.24 `examples.innerProductLogSig`/multiplyOut().(expnscip-python python iisignature 0.24 `examples.innerProductLogSig`/multiplyOut().(expn), dimscip-python python iisignature 0.24 `examples.innerProductLogSig`/multiplyOut().(dim)): |
| 46 | if isinstance(expnscip-python python iisignature 0.24 `examples.innerProductLogSig`/multiplyOut().(expn), listscip-python python python-stdlib 3.11 builtins/list#): |
| 47 | left, leftDepth = multiplyOutscip-python python iisignature 0.24 `examples.innerProductLogSig`/multiplyOut().(expnscip-python python iisignature 0.24 `examples.innerProductLogSig`/multiplyOut().(expn)[0], dimscip-python python iisignature 0.24 `examples.innerProductLogSig`/multiplyOut().(dim)) |
| 48 | right, rightDepth = multiplyOutscip-python python iisignature 0.24 `examples.innerProductLogSig`/multiplyOut().(expnscip-python python iisignature 0.24 `examples.innerProductLogSig`/multiplyOut().(expn)[1], dimscip-python python iisignature 0.24 `examples.innerProductLogSig`/multiplyOut().(dim)) |
| 49 | a = numpyscip-python python numpy 2.4.6 numpy/__init__:.outerscip-python python numpy 2.4.6 `numpy._core.numeric`/outer().(left, right).flatten() |
| 50 | b = numpyscip-python python numpy 2.4.6 numpy/__init__:.outerscip-python python numpy 2.4.6 `numpy._core.numeric`/outer().(right, left).flatten() |
| 51 | return a - b, leftDepth + rightDepth |
| 52 | else: |
| 53 | a = numpyscip-python python numpy 2.4.6 numpy/__init__:.zerosscip-python python iisignature 0.24 `numpy._core.multiarray`/zeros#(dimscip-python python iisignature 0.24 `examples.innerProductLogSig`/multiplyOut().(dim)) |
| 54 | a[expnscip-python python iisignature 0.24 `examples.innerProductLogSig`/multiplyOut().(expn) - 1] = 1 |
| 55 | return a, 1 |
| 56 | |
| 57 | |
| 58 | # string of bracketed expression, dim -> numpy array of its value, depth |
| 59 | # for example: |
| 60 | # valueOfBracket("[1,2]",2) is ([0,1,-1,0],2) |
| 61 | def valueOfBracketscip-python python iisignature 0.24 `examples.innerProductLogSig`/valueOfBracket().(textscip-python python iisignature 0.24 `examples.innerProductLogSig`/valueOfBracket().(text), dimscip-python python iisignature 0.24 `examples.innerProductLogSig`/valueOfBracket().(dim)): |
| 62 | return multiplyOutscip-python python iisignature 0.24 `examples.innerProductLogSig`/multiplyOut().(parseBracketedExpressionscip-python python iisignature 0.24 `examples.innerProductLogSig`/parseBracketedExpression().(textscip-python python iisignature 0.24 `examples.innerProductLogSig`/valueOfBracket().(text), 0)[0], dimscip-python python iisignature 0.24 `examples.innerProductLogSig`/valueOfBracket().(dim)) |
| 63 | |
| 64 | |
| 65 | def getMatricesscip-python python iisignature 0.24 `examples.innerProductLogSig`/getMatrices().(sscip-python python iisignature 0.24 `examples.innerProductLogSig`/getMatrices().(s)): |
| 66 | info = iisignature.info(sscip-python python iisignature 0.24 `examples.innerProductLogSig`/getMatrices().(s)) |
| 67 | m = info["level"] |
| 68 | d = info["dimension"] |
| 69 | values = [[] for i in rangescip-python python python-stdlib 3.11 builtins/range#(m)] |
| 70 | for exp in iisignature.basis(sscip-python python iisignature 0.24 `examples.innerProductLogSig`/getMatrices().(s)): |
| 71 | vec, level = valueOfBracketscip-python python iisignature 0.24 `examples.innerProductLogSig`/valueOfBracket().(exp, d) |
| 72 | values[level - 1].appendscip-python python python-stdlib 3.11 builtins/list#append().(vec) |
| 73 | out = [] |
| 74 | for v in values: |
| 75 | |
| 76 | def fscip-python python iisignature 0.24 `examples.innerProductLogSig`/getMatrices().f().(xscip-python python iisignature 0.24 `examples.innerProductLogSig`/getMatrices().f().(x), yscip-python python iisignature 0.24 `examples.innerProductLogSig`/getMatrices().f().(y)): |
| 77 | return numpyscip-python python numpy 2.4.6 numpy/__init__:.innerscip-python python numpy 2.4.6 `numpy._core.multiarray`/inner().(v[xscip-python python iisignature 0.24 `examples.innerProductLogSig`/getMatrices().f().(x)], v[yscip-python python iisignature 0.24 `examples.innerProductLogSig`/getMatrices().f().(y)]) |
| 78 | |
| 79 | out.appendscip-python python python-stdlib 3.11 builtins/list#append().(numpyscip-python python numpy 2.4.6 numpy/__init__:.fromfunctionscip-python python numpy 2.4.6 `numpy._core.numeric`/fromfunction().(numpyscip-python python numpy 2.4.6 numpy/__init__:.vectorizescip-python python iisignature 0.24 `numpy.lib._function_base_impl`/vectorize#(fscip-python python iisignature 0.24 `examples.innerProductLogSig`/getMatrices().f().), (len(v), len(v)), dtype=intscip-python python python-stdlib 3.11 builtins/int#)) |
| 80 | # store=[] |
| 81 | # for x1 in v: |
| 82 | # store.append([]) |
| 83 | # for x2 in v: |
| 84 | # store[-1].append(numpy.inner(x1,x2)) |
| 85 | # out.append(numpy.array(store)) |
| 86 | return out |
| 87 | |
| 88 | |
| 89 | def demoscip-python python iisignature 0.24 `examples.innerProductLogSig`/demo().(): |
| 90 | # 1. generate some dummy data |
| 91 | d = 3 |
| 92 | m = 6 |
| 93 | path1 = np.random.uniform(size=(20, d)) |
| 94 | path2 = np.random.uniform(size=(20, d)) |
| 95 | paths = (path1, path2) |
| 96 | s = iisignature.prepare(d, m) |
| 97 | |
| 98 | # 2. print the dot product of the log signatures in tensor space |
| 99 | # (The "x" means that the log signature is returned in tensor space.) |
| 100 | expandedLogSig1, expandedLogSig2 = (iisignature.logsig(i, s, "x") for i in paths) |
| 101 | target = np.innerscip-python python numpy 2.4.6 `numpy._core.multiarray`/inner().(expandedLogSig1, expandedLogSig2) |
| 102 | print_("Target:", floatscip-python python python-stdlib 3.11 builtins/float#(target)) |
| 103 | |
| 104 | # 3. use getMatrices to act on the log signatures expressed in a basis |
| 105 | # and get the same answer. |
| 106 | matrices = getMatricesscip-python python iisignature 0.24 `examples.innerProductLogSig`/getMatrices().(s) |
| 107 | logsig1, logsig2 = (iisignature.logsig(i, s) for i in paths) |
| 108 | adjustment = scipy.linalg.block_diag(*matrices) |
| 109 | # print(np.dot(adjustment,logsig2).shape) |
| 110 | print_("We get:", floatscip-python python python-stdlib 3.11 builtins/float#(np.innerscip-python python numpy 2.4.6 `numpy._core.multiarray`/inner().(logsig1, np.dotscip-python python numpy 2.4.6 `numpy._core.multiarray`/dot().(adjustment, logsig2)))) |
| 111 | |
| 112 | |
| 113 | if __name__scip-python python iisignature 0.24 `examples.innerProductLogSig`/ == "__main__": |
| 114 | demoscip-python python iisignature 0.24 `examples.innerProductLogSig`/demo().() |