2016-07-23 10 views
1

Während mein Problem mit der sympy github Problem here verknüpft scheint, scheint es entweder nicht direkt verwandt oder bleibt ein offenes, ungelöstes Problem (ab 2012).sympy Matrizen in einem sympy Matrix bleibt bei Substitution nicht bewertet

Mein Problem ist, dass ich eine 'Matrix' (wirklich, es ist nur ein Vektor, aber ist meine einzige Option, wie Sympy mit Dingen handelt) innerhalb meiner Matrix durch ein Symbol dargestellt, dass, wenn substituiert, zeigt eine Matrix anstatt es zu bewerten.

Unten ist ein Beispiel Codeblock, der diese demonstrieren: in jedes Element unterteilt werden

import numpy as np 
import sympy 
x = np.random.rand(15, 3) 
l_syms = sympy.MatrixSymbol('l', 1, 3) 
m = sympy.Matrix(x/l_syms) 
m.subs({l_syms:sympy.Matrix([[1,1,1]])}) 

Jeder Wert in der matrix [[1,1,1]] der m Matrix sollte, aber die Die Matrix bleibt unverändert, wobei die Indexnotation erhalten bleibt.

Matrix([ 
[ 0.98979011265311*Matrix([[1, 1, 1]])[0, 0]**(-1), 0.833576329284127*Matrix([[1, 1, 1]])[0, 1]**(-1), 0.575808048824554*Matrix([[1, 1, 1]])[0, 2]**(-1)], 
[ 0.523263044342704*Matrix([[1, 1, 1]])[0, 0]**(-1), 0.320197709246721*Matrix([[1, 1, 1]])[0, 1]**(-1), 0.592365354846089*Matrix([[1, 1, 1]])[0, 2]**(-1)], 
[ 0.943288501064919*Matrix([[1, 1, 1]])[0, 0]**(-1), 0.996020450969247*Matrix([[1, 1, 1]])[0, 1]**(-1), 0.964522394691641*Matrix([[1, 1, 1]])[0, 2]**(-1)], 
[ 0.763752929521655*Matrix([[1, 1, 1]])[0, 0]**(-1), 0.690054409108757*Matrix([[1, 1, 1]])[0, 1]**(-1), 0.417629855595703*Matrix([[1, 1, 1]])[0, 2]**(-1)], 
[ 0.213578356927868*Matrix([[1, 1, 1]])[0, 0]**(-1), 0.362782611339912*Matrix([[1, 1, 1]])[0, 1]**(-1), 0.892921288169683*Matrix([[1, 1, 1]])[0, 2]**(-1)], 
[ 0.068921237699985*Matrix([[1, 1, 1]])[0, 0]**(-1), 0.244310349677818*Matrix([[1, 1, 1]])[0, 1]**(-1), 0.977494896836049*Matrix([[1, 1, 1]])[0, 2]**(-1)], 
[0.0169721915631557*Matrix([[1, 1, 1]])[0, 0]**(-1), 0.254316922886399*Matrix([[1, 1, 1]])[0, 1]**(-1), 0.682785271511585*Matrix([[1, 1, 1]])[0, 2]**(-1)], 
[ 0.768287921847173*Matrix([[1, 1, 1]])[0, 0]**(-1), 0.679243253034139*Matrix([[1, 1, 1]])[0, 1]**(-1), 0.790710466097621*Matrix([[1, 1, 1]])[0, 2]**(-1)], 
[ 0.892115183428169*Matrix([[1, 1, 1]])[0, 0]**(-1), 0.988514816033581*Matrix([[1, 1, 1]])[0, 1]**(-1), 0.537769900907173*Matrix([[1, 1, 1]])[0, 2]**(-1)], 
[ 0.275725750770885*Matrix([[1, 1, 1]])[0, 0]**(-1), 0.928279364723852*Matrix([[1, 1, 1]])[0, 1]**(-1), 0.18023576064915*Matrix([[1, 1, 1]])[0, 2]**(-1)], 
[ 0.71421202332017*Matrix([[1, 1, 1]])[0, 0]**(-1), 0.663585719630706*Matrix([[1, 1, 1]])[0, 1]**(-1), 0.43460735199406*Matrix([[1, 1, 1]])[0, 2]**(-1)], 
[ 0.770814091341355*Matrix([[1, 1, 1]])[0, 0]**(-1), 0.0650430822905173*Matrix([[1, 1, 1]])[0, 1]**(-1), 0.0845773234523002*Matrix([[1, 1, 1]])[0, 2]**(-1)], 
[0.0398441324212175*Matrix([[1, 1, 1]])[0, 0]**(-1), 0.0358479090409692*Matrix([[1, 1, 1]])[0, 1]**(-1), 0.0801076763216808*Matrix([[1, 1, 1]])[0, 2]**(-1)], 
[ 0.89937521111821*Matrix([[1, 1, 1]])[0, 0]**(-1), 0.21500916688666*Matrix([[1, 1, 1]])[0, 1]**(-1), 0.661216039738327*Matrix([[1, 1, 1]])[0, 2]**(-1)], 
[ 0.866641224226343*Matrix([[1, 1, 1]])[0, 0]**(-1), 0.0506005171711028*Matrix([[1, 1, 1]])[0, 1]**(-1), 0.791277139679317*Matrix([[1, 1, 1]])[0, 2]**(-1)]]) 

ich einen Weg, nicht in der Lage sein scheinen zu finden, um bewerten diese alle Matrix-Objekte und deren Indizes - das einzige, was der Ferne über die Github Problem verknüpft verwandt war, aber xreplace ist mit für meine Absichten noch weniger optimal/korrekt, da es das Symbol an Ort und Stelle belässt, anstatt die Matrix zu ersetzen.

Jede Hilfe wird sehr geschätzt.

+0

bewerten Dies ist ein relevantes Thema https://github.com/sympy/sympy/issues/2962 – asmeurer

Antwort

1

doit wird dem Ausdruck

In [9]: print(m.subs({l_syms:sympy.Matrix([[1,1,1]])}).doit()) 
Matrix([[0.391482321650262, 0.261165517803277, 0.142349217414644], [0.273871679444907, 0.753721446526393, 0.915204734923647], [0.689284430582533, 0.509755263016457, 0.828178833602631], [0.258602302969241, 0.561852165820955, 0.926528186168350], [0.932268817842373, 0.275670112515102, 0.313790471533159], [0.815965494794080, 0.00638263457666399, 0.728986133320254], [0.771288596688822, 0.159602759327409, 0.846541783656596], [0.703501924167250, 0.168463436323684, 0.496465192945264], [0.265617558320534, 0.727288058464723, 0.301561940789455], [0.134747729067062, 0.315683443879649, 0.741893699540517], [0.763480667163729, 0.459612818589501, 0.295897102504639], [0.519647253027057, 0.884505816757734, 0.823849322619653], [0.0499379745242409, 0.333299403033741, 0.960531610086280], [0.658552664971765, 0.788381947436270, 0.609862319604282], [0.748840147759344, 0.434013189476355, 0.747584770109250]]) 
+0

Dank @asmeurer. Ich kam kurz darauf und habe vergessen, hierher zu kommen, um die Antwort zu posten. – mediantis