Using a for loop to interchange maximum and minimum variables in an equation

100 views Asked by At

I have unpacked multiple arrays containing numbers in a text file. I've already figured out to put them in my equation like so:

import numpy as np
import matplotlib.pyplot as plt
import urllib
from numpy import sin, cos
from scipy.special import jv

tms, period, perioderr, bjdo, bjdoerr, ecc, eccerr, omega, omegaerr, ampltd, ampltderr, yo, yoerr = np.loadtxt(TM_filename, unpack = True)

def compute_ETV(bjd, bjdo, period, ecc, ampltd, omega, yo, numiter = 20):
    M = 2 * np.pi * (bjd - bjdo) / period
    u = M + sum([2./k * jv(k,k*ecc) * sin(k*M) for k in range(1,numiter)])
    return yo + ampltd/(24*60*60) * ((1-ecc**2)**0.5 * sin(u)*cos(omega)+(cos(u)-ecc)*sin(omega))

for i,item in enumerate(tms[:10]):
    ETV = compute_ETV(bjd[ecl==0], bjdo[i], period[i], ecc[i], ampltd[i], omega[i], yo[i], 20)

The question is, "How can I interchange the minimum and maximum numbers of these values? I want to use a for loop to input maximums and minimums for each of the arrays with its corresponding error value by adding for the maximum or subtracting for the minimum, but how can I mix and match mins and maxs for every combination possible?

EDIT

Okay so people who don't like to find their through my mess of code, (It's a terrible habit, especially since I code...), I've thought of a TL:DR version.

I have these arrays: m, x, and b. After this, I have: m_error, x_error, and b_error. I set the min and max of these variables by adding and subtracting the error values of these original values.

m = np.arange(1, 10)
x = np.arange(1, 10)
b = np.arange(1, 10)
m_error = np.linspace(0, 1, 9)
x_error = np.linspace(0, 1, 9)
b_error = np.linspace(0, 1, 9)
m_max = m + m_error
m_min = m - m_error
x_max = x + x_error
x_min = x - x_error
b_max = b + b_error
b_min = b - b_error


def compute(m, x, b):
    y = m*x + b
    return y

How can I put in a loop that gives me y for: "m_min(x_min) + b_min", "m(x_min) + b_min", "m(x) + b(min)", "m(x) + b", "m_max(x) + b"... and so on?

1

There are 1 answers

0
ajsp On

You can find all possible permutations then find the numerical solutions by executing the strings; it's hacky, but if you need a better solution you can repost the question and wait on a reply (assuming a better solution exists!).

example:

m = 1
x = 2
b = 3
m_max = 4
m_min = 5
x_max = 6
x_min = 7
b_max = 8
b_min = 9

import parser

m_iters = ['m','m_max','m_min']
x_iters = ['x','x_max','x_min']
b_iters = ['b','b_max','b_min']
h =[]
i =[]

[h.append([k+'*'+j]) for k in m_iters for j in x_iters]     #m*x   perms
[i.append(k+[v for v in b_iters]) for k in h]               #m*x+b perms 
for k in range(1,4):
    for j in i:
        q = j[0]+'+'+j[k]
        ans_q = eval(parser.expr(j[0]+'+'+j[k]).compile())  #eqn from str
        print q, '=', ans_q

>>>
m*x+b = 5
m*x_max+b = 9
m*x_min+b = 10
m_max*x+b = 11
m_max*x_max+b = 27
m_max*x_min+b = 31
m_min*x+b = 13
m_min*x_max+b = 33
m_min*x_min+b = 38
m*x+b_max = 10
m*x_max+b_max = 14
m*x_min+b_max = 15
m_max*x+b_max = 16
m_max*x_max+b_max = 32
m_max*x_min+b_max = 36
m_min*x+b_max = 18
m_min*x_max+b_max = 38
m_min*x_min+b_max = 43
m*x+b_min = 11
m*x_max+b_min = 15
m*x_min+b_min = 16
m_max*x+b_min = 17
m_max*x_max+b_min = 33
m_max*x_min+b_min = 37
m_min*x+b_min = 19
m_min*x_max+b_min = 39
m_min*x_min+b_min = 44