python - Creating a matrix based on 3 inputs -


i need create 2 n matrix

    x   y 1  x1  y1 2  x2  y2 3  x3  y3 4  x4  y4 5  x5  y5 .  ..  .. n  xn  yn 

it needs created function 3 inputs: x,y, , n, let call make_m(x,y,n)

the matrix needs follow criteria:

  1. the sum of numbers in column x = x
  2. the sum of numbers in column y = y
  3. in given row, x#/y# = x/y
  4. there must n number of rows

now have function find possible pairs of x , y , return them in list of tuples, have no clue how approach question of finding tuples to put rows satisfy 4 requirements. here function:

def find_r(x,y):    return [(a, a*num2/num1) in range(1, num1) if (a*num2) % num1 == 0] 

yes, there examples wont work because of ratio, example:

in [60]: find_r(100,891) out[60]: [] 

and there wont work numbers of n. example following not possible values of n higher 4

in [57]: find_r(100,364) out[57]: [(25, 91), (50, 182), (75, 273)] 

but not worry now.

the real issue many resulting possibilities have large amount of possibilities, not need algorithmic function pick out correct tuples, best tuples.

i such tuples picks close each other in size if possible, meaning best solution of 1 tuple repeated entire sequence.

so how can write function able create matrix need?

what finding greatest common divisor (gcd) of x , y. tell maximum n still yields result, , in finding rows of matrix.

if divide both x , y gcd, lowest combination of x# , y# satisfies ratio requirement.. let's denote z. need find n numbers 1 gcd sum equals gcd. these numbers indicate multiples of z use rows of matrix.

example: use list x = 100, y = 364

in [57]: find_r(100,364) out[57]: [(25, 91), (50, 182), (75, 273)] 

with n = 2.

gcd(100, 364) = 4. in case, z = (25, 91). n = 2, need 2 numbers between 1 , 4 add 4. gives 2 combinations: either 2, 2 (using these multiply z 2 pairs of numbers: [(50, 182),(50, 182)]) or 1, 3 ([(25, 91), (75, 273)]).

for n = 3, solution 1, 1, 2; , n = 4 1, 1, 1, 1. higher n wouldn't yield result.

i hope helps. also, expects can use positive integers - task become trivial negative ones.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -