My computer programming knowledge is very limited so bear with me if I don't use the jargons. I'm dealing with python and basically there are two functions A and C, of which I want to create another function B to convert output of A to a suitable input of C. Note here all the letters are integers.
Output of A: L=[[a,b,c],[d,e,f],...] #finite list
L[0]=[a,b,c]
Now the problem is that input of C is in the form of (projective) coordinates.
Input of C: (x:y:z)
So I'm trying to create this bridge called function B between A and C with the following properties
Input of B:[x,a,b]
Output of B:(x/b:a/b:1)
So x/b and a/b here are rational numbers.
I don't know how much of this will help but I'm essentially trying to convert something I know from a database into something usable. At the moment this is what I've got that will give me the output of A.
D = CremonaDatabase()
x = EllipticCurve("389a1"); x
N = x.conductor(); N
y = str(N); y
z = len(y); z
a = x.cremona_label()[z:]; a
P = D.allgens(N)[a]; P
It seems like a very crude way of doing things. But it gets me where I want to so far.
I'd appreciate if somebody can tell me where to start looking.
you pretty much answered your own question.
I'm not sure what X is or where it is defined, but im just going to assume it's taken care of
def b(input):
return [[x/coord[1], coord[0]/coord[1], 1] for coord in input]
Related
I'm working a physics problem with complex numbers and think I'm setting everything up correctly but am obviously not doing something right along the way. It could be that I'm either not using the right equations or that I'm unfamiliar with how Python's handling the math, and I'm pretty sure I'm using the right stuff. I've already worked a problem using the same kind of procedure and got the correct value, so substituting my new values should
Given f = 1000, SWR = -5.9, L = 0.081, I apparently should be getting z = 1.4 - 0.23j.
Here's what I'm doing:
import numpy as np
import cmath
f = 1000 #frequency
SWR = -5.9
L = 0.081
w = 2*f*np.pi #angular frequency
c = 343 #speed of sound in air
k = w/c #wavenumber
BA = (SWR-1)/(SWR+1) #given
theta = 2*k*L-np.pi #given
z = (1+BA*np.e**(1j*theta))/(1-BA*np.e**(1j*theta)) #given
print(z)
This gives me z = (-4.699946746470462-2.3316919882323677j), obviously not what I'm being told is the correct value.
I've gone over this multiple times now and can't find anything wrong. I just again worked through the problem I already got correct and made the minor substitutions to fit these given values, and I'm still getting the returned value of z. I don't want to tell my professor his "check that your code is giving the correct results" result is wrong, but...
Am I missing something?
E: Apologies for the rough display, but I'm not sure I can type in LaTeX here. The following are what I'm working with. Furthermore, the final image shows that I worked basically the same problem correctly and that I should be able to just make some substitutions to work this one. Also note that in my code, z is actually z divided by the rhocS quantity. I'm after that, don't need to know their values.
Equation for z, BA, theta, and the worked similar problem
So, in my previous question wflynny gave me a really neat solution (Surface where height is a function of two functions, and a sum over the third). I've got that part working for my simple version, but now I'm trying to improve on this.
Consider the following lambda function:
x = np.arange(0,100, 0.1)
y = np.sin(y);
f = lambda xx: (xx-y[x=xx])**2
values = f(x)
Now, in this scenario it works. In fact, the [x=xx] is trivial in the example. However, the example can be extended:
x = np.arange(0,100, 0.1)
z = np.sin(y);
f = lambda xx, yy: ( (xx-z[x=xx])**2 + yy**2)**0.5
y = np.arange(0,100,0.1)
[xgrid, ygrid] = np.meshgrid(x,y);
values = f(xgrid,ygrid)
In this case, the error ValueError: boolean index array should have 1 dimension is generated. This is because z.shape is different from xgrid.shape, I think.
Note that here, y=np.sin(y) is a simplification. It's not a function but an array of arbitrary values. We really need to go to that array to retrieve them.
I do not know what the proper way to implement this is. I am going to try some things, but I hope that somebody here will give me hints or provide me with the proper way to do this in Python.
EDIT: I originally thought I had solved it by using the following:
retrieve = lambda pp: map(lambda pp: dataArray[pp==phiArray][0], phi)
However, this merely returns the dataArray. Suppose dataArray contains a number of 'maximum' values for the polar radius. Then, you would normally incorporate this by saying something like g = lambda xx, yy: f(xx,yy) * Heaviside( dataArray - radius(xx,yy)). Then g would properly be zero if the radius is too large.
However, this doesn't work. I'm not fully sure but the behaviour seems to be something like taking a single value of dataArray instead of the entire array.
Thanks!
EDIT: Sadly, this stuff has to work and I can't spend more time on making it nice. Therefore, I've opted for the dirty implementation. The actual thing I was interested in would be of the sort as the g = lambda xx, yy written above, so I can implement that directly (dirty) instead of nicely (without nested for loops).
def envelope(xx, yy):
value = xx * 0.
for i in range(0,N): #N is defined somewhere, and xx.shape = (N,N)
for j in range(0,N):
if ( dataArray[x=xx[i,j]][0] > radius(xx[i,j],yy[i,j])):
value[i,j] = 1.
else:
value[i,j] = 0.
return value
A last resort, but it works. And, sometimes results matter over writing good code, especially when there's a deadline coming up (and you are the only one that cares about good code).
I would still be very much interested in learning how to do this properly, if there is a proper way, and thus increase my fluency in clean Python.
I am trying to use numpy.mgrid to create two grid arrays, but I want a way to insert a variable as the number of steps.
Without a variable number of steps, numpy.mgrid works as expected with this code:
x, y = np.mgrid[0:1:3j, 0:2:5j]
But, what I want is something like this, because I am not able to explicitly state my step number in the line of code to generate the grid arrays (the values may change due to other processes in my script):
num_x_steps = 3
num_y_steps = 5
x, y = np.mgrid[0:1:(num_x_steps)j, 0:2:(num_y_steps)j] #Try convert to complex
Is there a way to do that, that returns a result equivalent to the first method?
I tried running my 3-line code with and without parentheses and tried a couple other modifications, but nothing seemed to work.
NOTE: I tried reading this topic, but I am not sure if what that topic deals with is applicable to this problem; I don't quite understand what is being asked or how it was answered. (Also, I tried running the line of code from the answer, and it returned a MemoryError.) If said topic does answer my problem, could someone please explain it better, and how it applies to my problem?
The glitch is that j following parentheses doesn't convert to a complex number.
In [41]:(1)j
File "<ipython-input-41-874f75f848c4>", line 1
(1)j
^
SyntaxError: invalid syntax
Multiplying a value by 1j will work, and these lines give x, y equivalent to your first line:
num_x_steps = 3
num_y_steps = 5
x, y = np.mgrid[0:1:(num_x_steps * 1j), 0:2:(num_y_steps * 1j)]
I think what you are looking for is to convert the num of steps to a complex number.
num_x_steps = 3
x_steps = complex(str(num_x_steps) + "j")
num_y_steps = 5
y_steps = complex(str(num_y_steps) + "j")
x, y = np.mgrid[0:1:x_steps, 0:2:y_steps]
I have a project that one step of the process of it is to solve R(k,d,a),
where k means kth step.
My friend suggest me to do this in sympy ,but I don't know how to do it.
from sympy import *
k= symbols('k')
d= symbols('d')
a= symbols('a')
R= function('R')(k,d,a)
print R`
In fact I don't know how to define a function in sympy with class method...
and this expression is failure.
def R(k,d,a):
k:# of nth process
d:parameter in order to initializing
R(0,d,a) should be given
if k==0:
return 100*d
r=d*(1-(1-(d/R(k-1,d,a))**2)**0.5)**0.5
B=-3/2*d
D=(R(k-1,d,a))**3*(3*a*d/R(k-1,d,a)-2)+r**2*(r-3/2*d)
here I define R(k,d,a) with R(k-1,d,a),is it appropriate?
x^3+Bx^2+Cx+D=0 ,where c=0
x represent R(k,d,a) here.
x=symbols('x')
y=solve (x**3+x**2*B+D,x)
return max(y)
Here I want a list of y,and asking y in real number.
Later return the biggest one.
But I don't know how to realize it.
Finally,for each k ,I will need the other function to give a value that R(k,d,a) will be a parameter in it.I think I can do it by my self with for loop,it is not hard for me.
What is the hardest is how to get R(k,d,a).
I don't need complex root .But if I want ,how can I get them ?
Thank you for watching!
What you have looks basically OK. Three suggestions, however:
make sure your function always returns a SymPy value, e.g. don't return 100*d since d might be an int; return S(100)*d;
wherever you have division make sure that it is not suffering from Python trunction wherein 1/2 -> 0. e.g. write B = -S(3)/2*d instead of what you have (and use that B in your expression for D, writing (r+B) at the end of it;
max will not be able to sort the roots if complex roots are present so it would be better to select the real ones by hand: y=[i for i in solve (x**3+x**2*B+D,x) if i.is_real].
Give the Python function XOR(a,b) that returns the XOR(a,b) where a and b are integers. Submit a complete Python program that uses XOR in file xor.py
This is confusing to me, am I being asked to find integers for a and b
or is it saying a and b begin as integers? I'm generally very confused by
python so this probably seems like a simple question but I do not know where
to even begin with writing the code.
I know the outline of the code should be
def XOR(a,b):
# Your code here
nbr1 = 67
nbr2 = 73
print (XOR(nbr1, nbr2))
The ^ operator in Python is XOR, so you can just do:
def XOR(a,b):
return a ^ b
nbr1 = 67
nbr2 = 73
print (XOR(nbr1, nbr2))
The XOR is just a mathematical function like multiplication, subtraction, addition etc. You might be more familiar with to the power of then XOR. For example, 5 to the 2nd power is 5^2 or XOR(5,2). You just have to create a def called XOR which takes two arguments and returns one of them raised to the other. It's not specified which argument should be taken as the power so you can just take the 2nd one maybe. This is the code:
def XOR(num1,num2):
return num1^num2
print(XOR(5,2))
#prints 25
a and b are integers to begin with. XOR is shorthand for "Exclusive Or" or "Exclusive Disjunctive." Basically, this means a or b, but not a and b ( remember that OR can mean a or b or a and b). It sounds like you are being to write a function that when given two values returns one of the values. You might try this:
import random
XOR(a, b):
list = [a , b ]
return random.choice(list)