Traceback in Lagrange Interpolation code in Python - python

Can someone please explain where I went wrong? I used the same code for a different problem where i had to create arrays from the given specifications, but i dont understand where this code went wrong.

I think your problem is that you haven't quite got straight what your variables mean.
In the function definition, you use Xp to mean a single value.
You also define it as a single value.
However just before you call the function, you treat it as though it was a list:
[ F(X,Y,i) for i in Xp ]
One fix would be to set Xp = [302], not 302.
Preventing future similar errors
Even better would be to use a more mnemonic variable name, such as Xp_list, so that you don't fall into a similar trap in future. In my code I would typically call those variables:
x_list
y_list
xp_list
Or
xs
ys
xps

Related

Using global parameters of z3py

I'm trying to use SMT solver over a scheduling problem and could not find anything helping in the documentation.
It seems using following ways of setting parameters do not have any effect on the solver.
from z3 import *
set_param(logic="QF_UFIDL")
s = Optimize() # or even Solver()
or even
from z3 import *
s = Optimize()
s.set("parallel.enable", True)
So how can I set [global] parameters effectively in z3py. to be most specific I need to set parameters below:
parallel.enable=True
auto_confic=False
smtlib2_compliant=True
logic="QF_UFIDL"
Use global parameter statements like the following on separate lines before creating Solver or Optimize object:
set_param('parallel.enable', True)
set_param('parallel.threads.max', 4) # default 10000
To set non-global parameters specific to a Solver or Optimize object, you can use the help() function to show available parameters:
o = Optimize()
o.help()
s = Solver()
s.help()
The following example shows how to set an Optimize parameter:
opt = Optimize()
opt.set(priority='pareto')
Use set_param, as described here: https://z3prover.github.io/api/html/namespacez3py.html#a4ae524d5f91ad1b380d8821de01cd7c3
It isn't clear what's not working for you. Are you getting an error message back? From your description, I understand that the setting does indeed take place, but you don't see any change in behavior? For that, you'll have to provide a concrete example we can look at. Note that for most parameters, the effects will only be visible with benchmarks that trigger the option, and even then it'll be hard to tell what (if any) effect it had, unless you dig into verbose log output.
Also, parallel-solving features, which you seem to be interested in, isn't going to gain you much. See Section 9.2 of https://z3prover.github.io/papers/z3internals.html: Essentially it boils down to attempting to solve the same problem with different seeds to see if one of them goes faster. If you have many cores lying around it might be worth a try, but don't expect magic out of it.

Can I pass additional arguments into a scipy bvp function or boundary conditions? / SciPy questions from MATLAB

I'm working on converting a code that solves a BVP (Boundary Value Problem) from MATLAB to Python (SciPy). However, I'm having a little bit of trouble. I wanted to pass a few arguments into the function and the boundary conditions; so in MATLAB, it's something like:
solution = bvp4c(#(x,y)fun(x,y,args),#(ya,yb)boundarycond(ya,yb,arg1,args),solinit);
Where arg1 is a value, and args is a structure or a class instance. So I've been trying to do this on scipy, and the closest I get to is something like:
solBVP = solve_bvp(func(x,y,args), boundC(x,y,arg1,args), x, y)
But then it errors out saying that y is not defined (which it isn't, because it's the vector of first order DEs).
Has anyone tried to pass additional arguments into solve_bvp? If so, how did you manage to do it? I have a workaround right now essentially putting args and arg1 as global variables, but that seems incredibly sketchy if I want to make this code somewhat modular.
Thank you!

Turn off minor ticks on xaxis

I'm trying to simplify the look of a graph of mine. To this extent, I would like to set ticks only on definite points.
The 'native' plot, out of a df.groupby.max().plot() operation looks like this:
I don't like the fact that my data starts at 0.3 and ends at 0.6, but the graph is somewhat adding real estate there. To have the plot limited to the numbers I want, I do:
ax1.set_xlim(0.3,0.6)
Which however adds a series of intermediate points I wouldn't like to have:
Now, for some reason, halfway points appear. Note that they do not belong to the measured data.
I've then tried the recipes found - among other places - here
ax1.set_xticks = np.arange(0.3,0.6,0.1)
--> no change
ax1.xaxis.set_tick_params(which='minor',bottom=False)
--> no change
ax1.minorticks_off()
--> no change
I've run out of options and I'm not sure what I'm doing wrong here, any help appreciated.
OK,
thanks to #DavidG's hint I found the issue. It's maybe not subtle but worth mentioning. I can remove the whole thing if this turns out to be too trivial.
The issue was created by this wrong call to the ax.set_xticks() function:
ax1.set_xticks = np.arange(0.3,0.6,0.1)
Although I cited the place where I took this approach, I actually managed to implement it wrongly. The right way would have been:
ax1.set_xticks(np.arange(0.3,0.6,0.1))
So, actually, I wasn't seeing any change in the plot because I wasn't calling the function correctly.
But there's more.
My code was actually assigning the (name?) ax1.set_xticks to an np.array, so that when trying to then implement the correct syntax, I kept getting an error:
ax1.set_xticks([0.3,0.4,0.5,0.6])
Traceback (most recent call last):
File "<ipython-input-93-df3b8935eb28>", line 1, in <module>
ax1.set_xticks([0.3,0.4,0.5,0.6])
TypeError: 'numpy.ndarray' object is not callable
Even with a simple list, I was getting the error. This is because I had assigned the name ax1.set_xticks to, indeed, an np.array object.
Once reset the variable space and properly called the function, everything ran smoothly.

__xor__ in functools.reduce() causing error

I'm currently working in python 3 with the Discord API and I want to use a module that was written in python 2. I'm editing some of the code for it to work with python 3. Something I can't figure out is this:
odd_xor = reduce(__xor__, bh) >> 16
This works in python 2 but doesn't in python 3. The simple fix that I thought would work was:
odd_xor = functools.reduce(__xor__, bh) >> 16
but this gives me the error:
reduce() of empty sequence with no initial value
bh is initialized here:
# bh stands for binary hand, map to that representation
card_to_binary = HandEvaluator.Six.card_to_binary_lookup
bh = map(card_to_binary, hand)
I don't really understand what the code segment is trying to do which is why it is so difficult for me to find a solution to this problem. Any thoughts? Thanks in advance!
P.S. if there is an easier way to use python 2 modules with python 3 projects, please enlighten me.
In Python 3, map returns a lazy iterator (much like a generator), rather than a list the way it did in Python 2. This may be the cause of your issue.
In your code, you do map(card_to_binary, hand). If hand gets changed (e.g. emptied) before you use the result, you may not get what you want (e.g. the map may end up iterating over nothing).
To make the code work the same as Python 2, you can simply call list on the iterator you get from map:
bh = list(map(card_to_binary, hand))
Alternatively, you could change other code that modifies hand so that it does something different, perhaps making a new list, copying the list before modifying it, or only modifying hand in place after the reduce call has been completed.
The said error occurs when the sequence passed as the second argument of reduce is empty, which means bh is an empty sequence when the error occurs.
Since bh is initialized with another sequence, hand, you need to find out why hand is empty in the first place.

Pass a matrix to functions from different module (Python)

I'm working on my Computer Graphics homework. Since we're allowed to choose the PL we want, I decided this would be a good occasion to learn some Python, but I ran into some trouble, eventually.
In one module I have some functions like this:
def function1 (a, b, matrix):
...
function2 (matrix)
def function2(matrix):
...
function3(x,y,matrix):
def function3(x,y,matrix):
...
matrix[x][y] = something
Now, from a different module, I call function1. It should then call function2 passing it the matrix, which should in turn call function3 passing it the matrix. However, I get a list assignment index out of range when attempting to access matrix[x][y.
If I try to call them on a matrix from the same module, it will work, so I thought that the functions might not realize they are receiving a matrix. I changed the function definitions to something like
function2(matrix = [[]])
but I still get the same error. I'm kind of stuck.
Sorry everyone, you were right.
There was this one pixel in a 500x500, which was actually at matrix[249][500].
When I made the checks, I checked if they're <=500 instead of <500, don't know why.
Thanks, I was pretty sure I was screwing something else up, especially after I added my (faulty) tests, since this is my first time writing python.

Categories