だから、私は自分で有効な解決策を見つけました。それが最善の解決策かどうかはわかりませんが、機能的です。
私の質問に答えるために、 scipy.optimize.fsolve はパラメーター args = (ここに追加の引数) を取ります。ここにあらかじめ決められたパラメータを入れます。関数が呼び出されると、引数が最初に解析され、3 つの事前定義された値が適切な場所に配置されます。
残りの 6 つの変数はリストで渡され、残りのギャップを埋めるために繰り返されます。引数は変更されないため、各変数は常にマトリックス内の同じ場所に配置されます。
この方法を使用すると、任意の 3 つの行列要素を事前に決定することができ、fsolve は残りを決定しようとします。
fsolve の呼び出しステートメントは次のようになります。
paramSolve1, infodict, ier, mesg = scipy.optimize.fsolve(func,(i,i,i,i,i,i),args = (knownVals[0],knownVals[1],knownVals[2]), full_output = True, warning = False)
knwonVals は事前定義されたパラメーターのリストで、i は開始時の推測です (6 つの欠損パラメーターはすべて同じ開始時の推測になります)。full_output を使用すると、オプションの出力を返すことができます。warning = False を使用すると、解決策が見つからない場合に表示される警告メッセージがオフになります。詳細については、http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.htmlをご覧ください。
興味のある方のために、問題のコード全体を以下に示します。
import scipy
from scipy.optimize import fsolve
def func(params, *args):
c = propMatrix(createMatrix(args), params)
ans =(scipy.dot(c[:, 0],c[:, 1]), scipy.dot(c[:, 1],c[:, 2]), scipy.dot(c[:, 0],c[:, 2]),scipy.dot(c[:, 0],c[:, 0])-1,scipy.dot(c[:, 1],c[:, 1])-1,scipy.dot(c[:, 2],c[:, 2])-1)
return ans
def createMatrix(knownVals):
c = [['____', '____', '____'],['____', '____', '____'], ['____', '____', '____']]
for element in knownVals:
x, y, val = element
c[y][x] = float(val)
return c
def propMatrix(c, params):
for p in params:
assign = True
for x in range(3):
for y in range(3):
if c[x][y]=='____' and assign:
c[x][y] = float(p)
assign = False
return scipy.array(c)
def test(c):
v1 = c[:, 0]
v2 = c[:, 1]
v3 = c[:, 2]
h1 = c[0, :]
h2 = c[1, :]
h3 = c[2, :]
ans = (scipy.dot(v1,v1)-1, scipy.dot(v1,v2), scipy.dot(v1, v3), scipy.dot(v2, v2)-1, scipy.dot(v2, v3), scipy.dot(v3,v3)-1, scipy.dot(h1,h1)-1, scipy.dot(h1,h2), scipy.dot(h1, h3), scipy.dot(h2, h2)-1, scipy.dot(h2, h3), scipy.dot(h3,h3)-1)
return ans
def getInput():
knownVals = []
print """\n\nThis module analytically solves for the rotation matrix\n
First, enter 3 known values of the matrix:\n
x
1 2 3
1 | c11 c12 c13 |
y 2 | c21 c22 c23 |
3 | c31 c32 c33 |\n\n"""
for i in range(3):
invalid = True
print "Point Number %i:"%(i)
while invalid:
x = int(raw_input("\tx-coordinate:"))-1
if x>2 or x<0:
print "\tInvalid x-coordinate."
else:
invalid = False
invalid = True
while invalid:
y = int(raw_input("\ty-coordinate:"))-1
if y>2 or y<0:
print "\tInvalid y-coordinate."
else:
invalid = False
invalid = True
while invalid:
val = float(raw_input("\tValue:"))
if val>1 or val<-1:
print "\tInvalid value. Must be -1 <= value <= 1"
else:
invalid = False
knownVals.append((x, y, val))
c = createMatrix(knownVals)
print "Input Matrix:\n\n", scipy.array(c)
choice = raw_input("\nIs this correct (y/n)? ")
if choice == "y":
return knownVals
elif choice == "n":
return getInput()
def Main():
solution = False
knownVals = getInput()
for i in (-1,-.5,0,.5,1):
paramSolve1, infodict, ier, mesg = scipy.optimize.fsolve(func,(i,i,i,i,i,i),args = (knownVals[0],knownVals[1],knownVals[2]), full_output = True, warning = False)
if ier == 1:
print "\nInitial value: %r"%(i)
print propMatrix(createMatrix(knownVals),paramSolve1)
solution = True
if not solution:
print "Could not find a valid solution"
scipy.set_printoptions(precision = 4, suppress = True)
Main()