# Revision history [back]

### Using sympy in python is substitute values into a 10x10 matrix

Hi,

I'm trying to substitute values into a symbolic 10X10 antisymmetric matrix in an iterative code. The symbolic expressions happen to be pretty complicated algebraic expressions (with some trig functions). I need to perform at least 10,000-20,000 iterations to get a meaningful result. But, it's taking too long to perform the substitution - like about 100 seconds per iteration. An example of what the expressions look like is:

(-1024 x1^18 (-1 + 3 Sqrt[3] x2 - 12 x2^2 + 9 Sqrt[3] x2^3 - 12 x2^4 + 3 Sqrt[3] x2^5 - x2^6) + 9216 x1^17 (Sqrt[3] - 9 x2 + 12 Sqrt[3] x2^2 - 27 x2^3 + 12 Sqrt[3] x2^4 - 9 x2^5 + Sqrt[3] x2^6) + 2048 x1^15 (Sqrt[3] (157 + 33 y1^2) - 9 x2 (157 + 34 y1^2) - 9 x2^5 (157 + 36 y1^2) + Sqrt[3] x2^6 (157 + 36 y1^2) - 9 x2^3 (471 + 106 y1^2) + 3 Sqrt[3] x2^2 (628 + 139 y1^2) + 3 Sqrt[3] x2^4 (628 + 143 y1^2)) - 256 x1^16 (-463 - 33 y1^2 + 3 Sqrt[3] x2 (463 + 34 y1^2) + 3 Sqrt[3] x2^5 (463 + 36 y1^2) - x2^6 (463 + 36 y1^2) + 3 Sqrt[3] x2^3 (1389 + 106 y1^2) - 3 x2^2 (1852 + 139 y1^2) - 3 x2^4 (1852 + 143 y1^2)) + 448 x1^13 (-27 x2 (1998 + 1400 y1^2 + 171 y1^4) - 18 x2^5 (2997 + 2224 y1^2 + 288 y1^4) + 2 Sqrt[3] x2^6 (2997 + 2224 y1^2 + 288 y1^4) + Sqrt[3] (5994 + 4076 y1^2 + 483 y1^4) + 12 Sqrt[3] x2^4 (5994 + 4417 y1^2 + 568 y1^4) + 9 Sqrt[3] x2^2 (7992 + 5724 y1^2 + 715 y1^4) - 18 x2^3 (8991 + 6548 y1^2 + 832 y1^4)) - 64 x1^14 (-28986 - 11996 y1^2 - 483 y1^4 + 9 Sqrt[3] x2 (9662 + 4120 y1^2 + 171 y1^4) + 6 Sqrt[3] x2^5 (14493 + 6544 y1^2 + 288 y1^4) - 2 x2^6 (14493 + 6544 y1^2 + 288 y1^4) - 12 x2^4 (28986 + 12997 y1^2 + 568 y1^4) - 9 x2^2 (38648 + 16844 y1^2 + 715 y1^4) + 6 Sqrt[3] x2^3 (43479 + 19268 y1^2 + 832 y1^4)) - (1 + y1^2)^4 (3 + 4 y1^2)^2 (-27 (1 + y1^2)^3 + 27 Sqrt[3] x2 (1 + y1^2)^2 (3 + 4 y1^2) + 3 Sqrt[3] x2^5 (3 + 4 y1^2)^3 - x2^6 (3 + 4 y1^2)^3 - 9 x2^4 (3 + 4 y1^2)^2 (4 + 5 y1^2) + 3 Sqrt[3] x2^3 (3 + 4 y1^2)^2 (9 + 10 y1^2) - 27 x2^2 (12 + 43 y1^2 + 51 y1^4 + 20 y1^6)) + 96 x1^11 (-18 x2^5 (42629 + 64876 y1^2 + 27104 y1^4 + 2688 y1^6) + 2 Sqrt[3] x2^6 (42629 + 64876 y1^2 + 27104 y1^4 + 2688 y1^6) + Sqrt[3] (85258 + 118814 y1^2 + 45421 y1^4 + 4115 y1^6) - 9 x2 (85258 + 122460 y1^2 + 48255 y1^4 + 4508 y1^6) - 18 x2^3 (127887 + 190982 y1^2 + 78288 y1^4 + 7616 y1^6) + 6 Sqrt[3] x2^4 (170516 + 257681 y1^2 + 106904 y1^4 + 10528 y1^6) + 3 Sqrt[3] x2^2 (341032 + 500778 y1^2 + 201807 y1^4 + 19292 y1^6)) - 16 x1^12 (-568390 - 572358 y1^2 - 133327 y1^4 - 4115 y1^6 + 6 Sqrt[3] x2^5 (284195 + 312396 y1^2 + 79520 y1^4 + 2688 y1^6) - 2 x2^6 (284195 + 312396 y1^2 + 79520 y1^4 + 2688 y1^6) + 3 Sqrt[3] x2 (568390 + 589836 y1^2 + 141621 y1^4 + 4508 y1^6) + 6 Sqrt[3] x2^3 (852585 + 919710 y1^2 + 229712 y1^4 + 7616 y1^6) - 6 x2^4 (1136780 + 1240845 y1^2 + 313656 y1^4 + 10528 y1^6) - 3 x2^2 (2273560 + 2411778 y1^2 + 592197 y1^4 + 19292 y1^6)) + 4 x1 (1 + y1^2)^3 (3 + 4 y1^2) (54 Sqrt[3] (1 + y1^2)^3 (4 + 5 y1^2) - 9 x2^5 (3 + 4 y1^2)^3 (8 + 9 y1^2) + Sqrt[3] x2^6 (3 + 4 y1^2)^3 (8 + 9 y1^2) - 9 x2^3 (3 + 4 y1^2)^2 (72 + 163 y1^2 + 92 y1^4) - 27 x2 (1 + y1^2)^2 (72 + 183 y1^2 + 116 y1^4) + 3 Sqrt[3] x2^4 (3 + 4 y1^2)^2 (96 + 229 y1^2 + 136 y1^4) + 9 Sqrt[3] x2^2 (288 + 1371 y1^2 + 2438 y1^4 + 1919 y1^6 + 564 y1^8)) - 4 x1^10 (-4464109 - 8209956 y1^2 - 4643613 y1^4 - 825358 y1^6 - 22488 y1^8 + 3 Sqrt[3] x2 (4464109 + 8463400 y1^2 + 4934607 y1^4 + 904328 y1^6 + 25424 y1^8) - 12 x2^4 (4464109 + 8906927 y1^2 + 5468364 y1^4 + 1056272 y1^6 + 31360 y1^8) + 3 Sqrt[3] x2^5 (4464109 + 8970288 y1^2 + 5546016 y1^4 + 1078784 y1^6 + 32256 y1^8) - x2^6 (4464109 + 8970288 y1^2 + 5546016 y1^4 + 1078784 y1^6 + 32256 y1^8) + 3 Sqrt[3] x2^3 (13392327 + 26403976 y1^2 + 16016832 y1^4 + 3056256 y1^6 + 89600 y1^8) - 3 x2^2 (17856436 + 34613932 y1^2 + 20640831 y1^4 + 3870536 y1^6 + 111440 y1^8)) + 4 x1^9 (Sqrt[ 3] (2642249 + 6191484 y1^2 + 4843797 y1^4 + 1410890 y1^6 + 112440 y1^8) - 9 x2 (2642249 + 6383960 y1^2 + 5149047 y1^4 + 1546360 y1^6 + 127120 y1^8) + 12 Sqrt[3] x2^4 (2642249 + 6720793 y1^2 + 5709132 y1^4 + 1807120 y1^6 + 156800 y1^8) - 9 x2^5 (2642249 + 6768912 y1^2 + 5790624 y1^4 + 1845760 y1^6 + 161280 y1^8) + Sqrt[3] x2^6 (2642249 + 6768912 y1^2 + 5790624 y1^4 + 1845760 y1^6 + 161280 y1^8) - 9 x2^3 (7926747 + 19921784 y1^2 + 16719936 y1^4 + 5228160 y1^6 + 448000 y1^8) + 3 Sqrt[3] x2^2 (10568996 + 26113268 y1^2 + 21543015 y1^4 + 6619960 y1^6 + 557200 y1^8)) + 4 x1^7 (-27 x2 (501027 + 1823718 y1^2 + 2473855 y1^4 + 1518596 y1^6 + 399600 y1^8 + 31808 y1^10) + Sqrt[3] (1503081 + 5303465 y1^2 + 6975399 y1^4 + 4152399 y1^6 + 1059560 y1^8 + 81744 y1^10) - 9 x2^5 (1503081 + 5806532 y1^2 + 8360160 y1^4 + 5448576 y1^6 + 1523200 y1^8 + 129024 y1^10) + Sqrt[3] x2^6 (1503081 + 5806532 y1^2 + 8360160 y1^4 + 5448576 y1^6 + 1523200 y1^8 + 129024 y1^10) + 9 Sqrt[3] x2^2 (2004108 + 7462561 y1^2 + 10357007 y1^4 + 6506084 y1^6 + 1752560 y1^8 + 142912 y1^10) - 9 x2^3 (4509243 + 17084218 y1^2 + 24126464 y1^4 + 15423552 y1^6 + 4229120 y1^8 + 351232 y1^10) + 3 Sqrt[3] x2^4 (6012324 + 23058439 y1^2 + 32963632 y1^4 + 21333216 y1^6 + 5922560 y1^8 + 498176 y1^10)) + x1^8 (15307515 + 44486513 y1^2 + 45919065 y1^4 + 19771179 y1^6 + 3083480 y1^8 + 81744 y1^10 - 9 Sqrt[3] x2 (5102505 + 15293526 y1^2 + 16277569 y1^4 + 7226420 y1^6 + 1162320 y1^8 + 31808 y1^10) - 3 Sqrt[3] x2^5 (15307515 + 48668708 y1^2 + 54959136 y1^4 + 25898880 y1^6 + 4426240 y1^8 + 129024 y1^10) + x2^6 (15307515 + 48668708 y1^2 + 54959136 y1^4 + 25898880 y1^6 + 4426240 y1^8 + 129024 y1^10) + 9 x2^2 (20410020 + 62568169 y1^2 + 68123633 y1^4 + 30946580 y1^6 + 5095760 y1^8 + 142912 y1^10) - 3 Sqrt[3] x2^3 (45922545 + 143217994 y1^2 + 158651264 y1^4 + 73339200 y1^6 + 12293120 y1^8 + 351232 y1^10) + 3 x2^4 (61230060 + 193280767 y1^2 + 216723472 y1^4 + 101416800 y1^6 + 17212160 y1^8 + 498176 y1^10)) - 2 x1^2 (1 + y1^2)^2 (3 Sqrt[3] x2^5 (3 + 4 y1^2)^3 (729 + 1702 y1^2 + 1054 y1^4 + 72 y1^6) - x2^6 (3 + 4 y1^2)^3 (729 + 1702 y1^2 + 1054 y1^4 + 72 y1^6) - 27 (1 + y1^2)^3 (729 + 1876 y1^2 + 1268 y1^4 + 80 y1^6) + 3 Sqrt[3] x2^3 (3 + 4 y1^2)^2 (6561 + 22956 y1^2 + 27266 y1^4 + 11616 y1^6 + 736 y1^8) + 9 Sqrt[3] x2 (1 + y1^2)^2 (6561 + 25110 y1^2 + 32562 y1^4 + 15024 y1^6 + 928 y1^8) - 3 x2^4 (3 + 4 y1^2)^2 (8748 + 31533 y1^2 + 38558 y1^4 + 16888 y1^6 + 1088 y1^8) - 27 x2^2 (8748 + 52641 y1^2 + 127125 y1^4 + 154870 y1^6 + 96710 y1^8 + 26576 y1^10 + 1504 y1^12)) + 4 x1^3 (1 + y1^2) (3 Sqrt[ 3] (1 + y1^2)^3 (10476 + 27591 y1^2 + 20656 y1^4 + 3192 y1^6) - 9 x2^5 (3 + 4 y1^2)^2 (3492 + 12659 y1^2 + 16108 y1^4 + 8096 y1^6 + 1152 y1^8) + Sqrt[3] x2^6 (3 + 4 y1^2)^2 (3492 + 12659 y1^2 + 16108 y1^4 + 8096 y1^6 + 1152 y1^8) - 9 x2 (1 + y1^2)^2 (31428 + 121095 y1^2 + 162900 y1^4 + 85352 y1^6 + 11936 y1^8) - 9 x2^3 (94284 + 579429 y1^2 + 1444422 y1^4 + 1850832 y1^6 + 1263008 y1^8 + 419584 y1^10 + 48128 y1^12) + 3 Sqrt[3] x2^2 (125712 + 756486 y1^2 + 1846161 y1^4 + 2315495 y1^6 + 1546452 y1^8 + 502824 y1^10 + 56480 y1^12) + 3 Sqrt[3] x2^4 (125712 + 784062 y1^2 + 1983861 y1^4 + 2580416 y1^6 + 1787504 y1^8 + 602752 y1^10 + 70144 y1^12)) + 12 x1^5 (Sqrt[ 3] (1 + y1^2)^2 (120339 + 355559 y1^2 + 350553 y1^4 + 126884 y1^6 + 12352 y1^8) - 9 x2 (120339 + 615483 y1^2 + 1259097 y1^4 + 1300543 y1^6 + 698862 y1^8 + 177168 y1^10 + 14896 y1^12) - 9 x2^5 (120339 + 653975 y1^2 + 1421458 y1^4 + 1560032 y1^6 + 890848 y1^8 + 240128 y1^10 + 21504 y1^12) + Sqrt[3] x2^6 (120339 + 653975 y1^2 + 1421458 y1^4 + 1560032 y1^6 + 890848 y1^8 + 240128 y1^10 + 21504 y1^12) - 9 x2^3 (361017 + 1923433 y1^2 + 4099284 y1^4 + 4411888 y1^6 + 2470880 y1^8 + 653184 y1^10 + 57344 y1^12) + 3 Sqrt[3] x2^2 (481356 + 2519670 y1^2 + 5275836 y1^4 + 5578327 y1^6 + 3068910 y1^8 + 796752 y1^10 + 68656 y1^12) + 3 Sqrt[3] x2^4 (481356 + 2596654 y1^2 + 5603287 y1^4 + 6106024 y1^6 + 3462560 y1^8 + 926912 y1^10 + 82432 y1^12)) - 2 x1^6 (-2859759 - 12041841 y1^2 - 19649927 y1^4 - 15433943 y1^6 - 5816778 y1^8 - 875384 y1^10 - 24704 y1^12 + 3 Sqrt[3] x2 (2859759 + 12426372 y1^2 + 20918355 y1^4 + 16945346 y1^6 + 6585900 y1^8 + 1022304 y1^10 + 29792 y1^12) + 3 Sqrt[3] x2^5 (2859759 + 13195434 y1^2 + 23588228 y1^4 + 20293696 y1^6 + 8379840 y1^8 + 1383424 y1^10 + 43008 y1^12) - x2^6 (2859759 + 13195434 y1^2 + 23588228 y1^4 + 20293696 y1^6 + 8379840 y1^8 + 1383424 y1^10 + 43008 y1^12) + 3 Sqrt[3] x2^3 (8579277 + 38817240 y1^2 + 68050472 y1^4 + 57421472 y1^6 + 23256000 y1^8 + 3764992 y1^10 + 114688 y1^12) - 3 x2^2 (11439036 + 50859081 y1^2 + 87611721 y1^4 + 72637010 y1^6 + 28900140 y1^8 + 4594656 y1^10 + 137312 y1^12) - 3 x2^4 (11439036 + 52397205 y1^2 + 92995806 y1^4 + 79444976 y1^6 + 32577600 y1^8 + 5341056 y1^10 + 164864 y1^12)) + x1^4 (9 (1 + y1^2)^3 (94617 + 261296 y1^2 + 223202 y1^4 + 57072 y1^6 + 2128 y1^8) - 3 Sqrt[3] x2 (1 + y1^2)^2 (851553 + 3363444 y1^2 + 4806378 y1^4 + 2913880 y1^6 + 641456 y1^8 + 23872 y1^10) - 3 Sqrt[3] x2^5 (851553 + 5387004 y1^2 + 14000058 y1^4 + 19095880 y1^6 + 14405280 y1^8 + 5733504 y1^10 + 996352 y1^12 + 36864 y1^14) + x2^6 (851553 + 5387004 y1^2 + 14000058 y1^4 + 19095880 y1^6 + 14405280 y1^8 + 5733504 y1^10 + 996352 y1^12 + 36864 y1^14) - 3 Sqrt[3] x2^3 (2554659 + 15840558 y1^2 + 40357422 y1^4 + 53972748 y1^6 + 39926560 y1^8 + 15584832 y1^10 + 2655744 y1^12 + 96256 y1^14) + 3 x2^2 (3406212 + 20746881 y1^2 + 51920865 y1^4 + 68205382 y1^6 + 49557514 y1^8 + 18997656 y1^10 + 3178288 y1^12 + 112960 y1^14) + 3 x2^4 (3406212 + 21387789 y1^2 + 55178856 y1^4 + 74726074 y1^6 + 55976480 y1^8 + 22126176 y1^10 + 3818752 y1^12 + 140288 y1^14)))/((1 + Sqrt[3] x1 + x1^2 + y1^2)^6 (3 + 4 Sqrt[3] x1 + 4 x1^2 + 4 y1^2)^2 (4 x1^2 (-1 + Sqrt[3] x2 - x2^2) - 4 x1 (Sqrt[3] - 3 x2 + Sqrt[3] x2^2) - 3 (1 + y1^2) + Sqrt[3] x2 (3 + 4 y1^2) - x2^2 (3 + 4 y1^2))^3)

I'm sorry, this is what one of the entries looks like after simplifying it using Mathematica. The entries with trig expressions are more complicated and Mathematica cannot simplify them. The x1,y1 etc.. are the varibales

I was wondering if there is a faster way to perform the substitution using sympy. Right now, I'm only substituting the top half of the matrix since it's antisymmetric. But even that doesn't reduce the time needed... Thanks!