求解线性系统

时间:2017-07-09 16:59:18

标签: python sympy

我有16个方程式:

a, b, c, d, e, f, g = symbols('a, b, c, d, e, f, g', real=True)
z00, z01, z02, z03 = symbols('z00, z01, z02, z03', real=True)
z10, z11, z12, z13 = symbols('z10, z11, z12, z13', real=True)
z20, z21, z22, z23 = symbols('z20, z21, z22, z23', real=True)
z30, z31, z32, z33 = symbols('z30, z31, z32, z33', real=True)    

Eq(z00, g)
Eq(z03, b + d + f + g)
Eq(-z00 + 6*z01 - 12*z02 + 8*z03, 8*b + 4*d + 2*f + g)
Eq(-8*z00 + 36*z01 - 54*z02 + 27*z03, 27*b + 9*d + 3*f + g)
Eq(z30, a + c + e + g)
Eq(z33, a + b + c + d + e + f + g)
Eq(-z30 + 6*z31 - 12*z32 + 8*z33, a + 8*b + c + 4*d + e + 2*f + g)
Eq(-8*z30 + 36*z31 - 54*z32 + 27*z33, a + 27*b + c + 9*d + e + 3*f + g)
Eq(-z00 + 6*z10 - 12*z20 + 8*z30, 8*a + 4*c + 2*e + g)
Eq(-z03 + 6*z13 - 12*z23 + 8*z33, 8*a + b + 4*c + d + 2*e + f + g)
Eq(z00 - 6*z01 + 12*z02 - 8*z03 - 6*z10 + 36*z11 - 72*z12 + 48*z13 + 12*z20 - 72*z21 + 144*z22 - 96*z23 - 8*z30 + 48*z31 - 96*z32 + 64*z33, 8*a + 8*b + 4*c + 4*d + 2*e + 2*f + g)
Eq(8*z00 - 36*z01 + 54*z02 - 27*z03 - 48*z10 + 216*z11 - 324*z12 + 162*z13 + 96*z20 - 432*z21 + 648*z22 - 324*z23 - 64*z30 + 288*z31 - 432*z32 + 216*z33, 8*a + 27*b + 4*c + 9*d + 2*e + 3*f + g)
Eq(-8*z00 + 36*z10 - 54*z20 + 27*z30, 27*a + 9*c + 3*e + g)
Eq(-8*z03 + 36*z13 - 54*z23 + 27*z33, 27*a + b + 9*c + d + 3*e + f + g)
Eq(8*z00 - 48*z01 + 96*z02 - 64*z03 - 36*z10 + 216*z11 - 432*z12 + 288*z13 + 54*z20 - 324*z21 + 648*z22 - 432*z23 - 27*z30 + 162*z31 - 324*z32 + 216*z33, 27*a + 8*b + 9*c + 4*d + 3*e + 2*f + g)
Eq(64*z00 - 288*z01 + 432*z02 - 216*z03 - 288*z10 + 1296*z11 - 1944*z12 + 972*z13 + 432*z20 - 1944*z21 + 2916*z22 - 1458*z23 - 216*z30 + 972*z31 - 1458*z32 + 729*z33, 27*a + 27*b + 9*c + 9*d + 3*e + 3*f + g)

并希望根据z..ab,......来表达(解决)所有c

所以我所做的就是取左右手边的方程式并将它们变成矩阵,如下所示:

AA = Matrix([[z00], [z03], [-z00 + 6*z01 - 12*z02 + 8*z03], [-8*z00 + 36*z01 - 54*z02 + 27*z03], [z30], [z33], [-z30 + 6*z31 - 12*z32 + 8*z33], [-8*z30 + 36*z31 - 54*z32 + 27*z33], [-z00 + 6*z10 - 12*z20 + 8*z30], [-z03 + 6*z13 - 12*z23 + 8*z33], [z00 - 6*z01 + 12*z02 - 8*z03 - 6*z10 + 36*z11 - 72*z12 + 48*z13 + 12*z20 - 72*z21 + 144*z22 - 96*z23 - 8*z30 + 48*z31 - 96*z32 + 64*z33], [8*z00 - 36*z01 + 54*z02 - 27*z03 - 48*z10 + 216*z11 - 324*z12 + 162*z13 + 96*z20 - 432*z21 + 648*z22 - 324*z23 - 64*z30 + 288*z31 - 432*z32 + 216*z33], [-8*z00 + 36*z10 - 54*z20 + 27*z30], [-8*z03 + 36*z13 - 54*z23 + 27*z33], [8*z00 - 48*z01 + 96*z02 - 64*z03 - 36*z10 + 216*z11 - 432*z12 + 288*z13 + 54*z20 - 324*z21 + 648*z22 - 432*z23 - 27*z30 + 162*z31 - 324*z32 + 216*z33], [64*z00 - 288*z01 + 432*z02 - 216*z03 - 288*z10 + 1296*z11 - 1944*z12 + 972*z13 + 432*z20 - 1944*z21 + 2916*z22 - 1458*z23 - 216*z30 + 972*z31 - 1458*z32 + 729*z33]])
bb = Matrix([[g], [b + d + f + g], [8*b + 4*d + 2*f + g], [27*b + 9*d + 3*f + g], [a + c + e + g], [a + b + c + d + e + f + g], [a + 8*b + c + 4*d + e + 2*f + g], [a + 27*b + c + 9*d + e + 3*f + g], [8*a + 4*c + 2*e + g], [8*a + b + 4*c + d + 2*e + f + g], [8*a + 8*b + 4*c + 4*d + 2*e + 2*f + g], [8*a + 27*b + 4*c + 9*d + 2*e + 3*f + g], [27*a + 9*c + 3*e + g], [27*a + b + 9*c + d + 3*e + f + g], [27*a + 8*b + 9*c + 4*d + 3*e + 2*f + g], [27*a + 27*b + 9*c + 9*d + 3*e + 3*f + g]])

然后我尝试了:

from sympy.solvers.solveset import linsolve
linsolve((AA, bb), [z00])
linsolve((AA, bb), [z00, z01, z02, z03, z10, z11, z12, z13, z20, z21, z22, z23, z30, z31, z32, z33])

但我得到了EmptySet()。我知道至少z00应该等于g所以我期待至少某种非空答案。

拜托,有没有人知道如何解决这些方程式,即为什么我会得到EmptySet()

1 个答案:

答案 0 :(得分:1)

尝试#!/usr/bin/php -q <?php # # Configuration: Enter the url and key. That is it. # url => URL to api/task/cron e.g # http://yourdomain.com/support/api/tickets.json # key => API's Key (see admin panel on how to generate a key) # $data add custom required fields to the array. # # Originally authored by jared@osTicket.com # Modified by ntozier@osTicket / tmib.net // If 1, display things to debug. $debug="0"; // You must configure the url and key in the array below. $config = array( 'url'=>'http://site.tld/api/tickets.json', // URL to site.tld/api/tickets.json 'key'=>'27ADED3FED6AC18D8068FCB942DB4442' // API Key goes here ); # NOTE: some people have reported having to use "http://your.domain.tld/api/http.php/tickets.json" instead. if($config['url'] === 'http://site.tld/api/tickets.json') { echo "<p style=\"color:red;\"><b>Error: No URL</b><br>You have not configured this script with your URL!</p>"; echo "Please edit this file ".__FILE__." and add your URL at line 18.</p>"; die(); } if(IsNullOrEmptyString($config['key']) || ($config['key'] === '27ADED3FED6AC18D8068FCB942DB4442')) { echo "<p style=\"color:red;\"><b>Error: No API Key</b><br>You have not configured this script with an API Key!</p>"; echo "<p>Please log into osticket as an admin and navigate to: Admin panel -> Manage -> Api Keys then add a new API Key.<br>"; echo "Once you have your key edit this file ".__FILE__." and add the key at line 19.</p>"; die(); } # Fill in the data for the new ticket, this will likely come from $_POST. # NOTE: your variable names in osT are case sensiTive. # So when adding custom lists or fields make sure you use the same case # For examples on how to do that see Agency and Site below. $data = array( 'name' => 'John Doe', // from name aka User/Client Name 'email' => 'john@gmail.com', // from email aka User/Client Email 'phone' => '1234567890', // phone number aka User/Client Phone Number 'subject' => 'Test API message', // test subject, aka Issue Summary 'message' => 'This is a test of the osTicket API', // test ticket body, aka Issue Details. 'ip' => $_SERVER['REMOTE_ADDR'], // Should be IP address of the machine thats trying to open the ticket. 'topicId' => '1', // the help Topic that you want to use for the ticket //'Agency' => '58', //this is an example of a custom list entry. This should be the number of the entry. //'Site' => 'Bermuda'; // this is an example of a custom text field. You can push anything into here you want. 'attachments' => array() ); # more fields are available and are documented at: # https://github.com/osTicket/osTicket-1.8/blob/develop/setup/doc/api/tickets.md if($debug=='1') { print_r($data); die(); } # Add in attachments here if necessary # Note: there is something with this wrong with the file attachment here it does not work. $data['attachments'][] = array('file.txt' => 'data:text/plain;base64;' .base64_encode(file_get_contents('/file.txt'))); // replace ./file.txt with /path/to/your/test/filename.txt #pre-checks function_exists('curl_version') or die('CURL support required'); function_exists('json_encode') or die('JSON support required'); #set timeout set_time_limit(30); #curl post $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $config['url']); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket API Client v1.8'); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:', 'X-API-Key: '.$config['key'])); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $result=curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($code != 201) die('Unable to create ticket: '.$result); $ticket_id = (int) $result; # Continue onward here if necessary. $ticket_id has the ID number of the # newly-created ticket function IsNullOrEmptyString($question){ return (!isset($question) || trim($question)===''); } ?>

solve()

不确定为什么_Z = symbols('z0(0:4),z1(0:4),z2(0:4),z3(0:4)', real=True) solve(AA - bb, _Z) 如下所示会返回linsolve(),但我猜测它与表达式中符号的顺序有关。

EmptySet

但下面的代码可以使用

linsolve(AA - bb, _Z)