How to write program run matrix as below in python?

2024/11/18 6:22:35

Thanks for everyone's reply. I will explain here. Suppose there is a given matrix

    x                  y                B = [5,-4,5,-6]
[[0,0,0,0],        [[0,1,0,1],[0,0,0,0],         [0,0,0,0],[0,0,0,0],         [0,0,0,1],[0,0,0,0]]         [0,0,0,0]]

for example a feasible solution is [[0,4,0,1],[0,0,0,0],[0,0,0,5],[0,0,0,0]] 4+1-0 == 5 0-4 == -4 5-0 == 5 0 - 5-1 == -6

I want to update x to make sure:

 (1) if y[i][j] == 0:x[i][j] = 0(2) x[0][0]+x[0][1]+x[0][2]+x[0][3]-x[0][0]-x[1][0]-x[2][0]-x[3][0] = B[0]x[1][0]+x[1][1]+x[1][2]+x[1][3]-x[0][1]-x[1][1]-x[2][1]-x[3][1] = B[1]...

How to program to find the feasible x?

Answer

answer updated, i wrote some code to parse variables.

B = [5,-4,5,-6]
y = [[0,1,0,1],[0,0,0,0],[0,0,0,1],[0,0,0,0],]
x = []
for i, row in enumerate(y):temp = []for j, col in enumerate(row):if col != 0:temp.append(str(col) + '*x' + str(i) + str(j))else:temp.append(col)x.append(temp)#for one in x:
#    print oneequ = []
for i in xrange(4):temp1 = []temp2 = []for j in xrange(4):temp1.append(x[i][j])temp2.append(x[j][i])temp2.append(B[i])equ.append(tuple(temp1 + temp2))equtions = []
for one in equ:s = '%s + %s + %s + %s - %s - %s - %s - %s = %s' % oneequtions.append(s)for one in equtions:print oneimport re
from copy import deepcopyequ_bak = deepcopy(equtions)p_var = re.compile(r'x\d\d')
vars = set([])
for one in equ_bak:m = p_var.findall(one)vars |= set(m)
vars = sorted(list(vars))p_ef = re.compile(r'([+-]* *\d*)\*(x\d\d)')
effs = []
for one in equ_bak:m = p_ef.findall(one)#print mtemp = [0] * len(vars)for num, var in m:try:temp[vars.index(var)] = float(num.replace(' ', ''))except:passeffs.append(tuple(temp))#for one in effs:
#    print oneimport numpy as np
A = np.array(effs)
x = np.linalg.lstsq(A,B)
print vars
print x[0]
https://en.xdnf.cn/q/120106.html

Related Q&A

Scraping Dynamic Information

I recently started with coding, I use Python and Pycharm. I Installed and imported the needed "Add-ons" like Selenium. For my first project I tried to get the "address" information …

Combining three RGB images into a single RGB image

I have three RGB images, but each one has only 1 non-zero channel (ie. one has a red channel with 0s in the blue and green channels) and I want to combine them into a single RGB image with the correct …

Parse XML to Table in Python

Im trying to parse XML to table-like structure in Python. Imagine XML like this:<?xml version="1.0" encoding="UTF-8"?> <base><element1>element 1</element1>…

Use Artificial Intelligence to predict next number (n+1) in a sequence

The AI must predict the next number in a given sequence of incremental integers using Python, but so far I havent gotten the intended result. I tried changing the learning rate and iterations but so fa…

Calculating size folder python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 8…

Answer Error, Only outputting Zero

I am coding in python and I cannot seem to figure out why when the amount of tickets sold is entered it does not calculate the full price of the tickets that were sold. Any help is appreciated, Thanks.…

finding a pattern match and concatenating the rest of lines in python

I have a small data set to clean. I have opened the text file in Pycharm. The data set is like this:Code-6667+ Name of xyz company+ Address + Number+ Contact person+ Code-6668+ Name of abc company, A…

Flat a list of containing only one dict

I have a dict which contain a list product which will contain only one dict: d = {"thickness": 90.0,"mass_surf": 37.8,"res_therm": 0.75,"codename": "codenam…

How to breakup a list of list in a given way in Python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Incrementing a counter while assigning it in python

Is it possible to do the following in python somehow?nodename = node%s % (++num if ct[0] != J else num)Or do I need to first do the increment and the assign it on the next line:if ct[0] != J:num += 1n…