Sorry for the messy title, but let me explain. I have a long code doing a bunch of manipulations on an expressions including (among others) a function f=sp.Function('f')(x,y). My end result is some huge expression, but I noted that there are terms that look like
f^3(x,y)f(x,y)-f^4(x,y)
and it completely fails to simplify this to zero. This makes the expressions much longer than they should be and thus very had to handle. Does anyone recognize this problem? How can I force sympy to know that f and f are the same function?
I can not provide a working example since the code is very long, and I was not able to identify a small subcode that gives the same problem. If you need the output or have some idea how I can manipulate the output to identify what the problem is, please tell me.
Thanks.
Try using simplify function on the end result.
Related
I can't seem to find the code for numpy argmax.
The source link in the docs lead me to here, which doesn't have any actual code.
I went through every function that mentions argmax using the github search tool and still no luck. I'm sure I'm missing something.
Can someone lead me in the right direction?
Thanks
Numpy is written in C. It uses a template engine that parsing some comments to generate many versions of the same generic function (typically for many different types). This tool is very helpful to generate fast code since the C language does not provide (proper) templates unlike C++ for example. However, it also make the code more cryptic than necessary since the name of the function is often generated. For example, generic functions names can look like #TYPE#_#OP# where #TYPE# and #OP# are two macros that can take different values each. On top of all of this, the CPython binding also make the code more complex since C functions have to be wrapped to be called from a CPython code with complex arrays (possibly with a high amount of dimensions and custom user types) and CPython arguments to decode.
_PyArray_ArgMinMaxCommon is a quite good entry point but it is only a wrapping function and not the main computing one. It is only useful if you plan to change the prototype of the Numpy function from Python.
The main computational function can be found here. The comment just above the function is the one used to generate the variants of the functions (eg. CDOUBLE_argmax). Note that there are some alternative specific implementation for alternative type below the main one like OBJECT_argmax since CPython objects and strings must be computed a bit differently. Thank you for contributing to Numpy.
As mentioned in the comments, you'll likely find what you are searching in the C code implementation (here under _PyArray_ArgMinMaxCommon). The code itself can be very convoluted, so if your intent was to open an issue on numpy with a broad idea, I would do it on the page you linked anyway.
In this quiz, we aren't asking you to solve problems. Instead, you are
given some problems and their incorrect solutions, and your task is to
find tests where these solutions actually work incorrectly — by
returning incorrect answer, working for too long, crashing, taking too
much memory — anything that would give a verdict other than
"Accepted". Consider that the time limit is 2 seconds, and the memory
limit is 256 megabytes for all of the following problems.
This exercise will help you imagine tests for your own code. And it's
actually very similar to "challenges" on many competitive programming
platforms (e.g. Topcoder, Codeforces) — where after solving a problem
you could earn additional score by breaking other's solutions to it.
We hope you'll like it!
Next follows the first problem and the incorrect solution to it.
You are given a non-empty list of integers, and you need to find the
maximum value among them. The length of the list is not greater than
100, and the absolute value of each element is not greater than 1000.
def solve(a):
max = 0
for x in a:
if x > max:
max = x
return max
Implement a function called getTest. It should return a list on which
the solve function works incorrectly. Note that the returned list must
fit the restrictions in the statement.
The function is to be implemented in Python 3, but if you don't know
this language, it's no problem — the sample code should give you the
idea of how to do what you need.
def getTest():
return [1,2,3];
I need to find the list where solve function fails but I am unable to do it since I am new to programming. Can anyone help me understand it?
Since this smells like homework, I'll give you a hint rather than the full solution. You don't need to worry about being new to programming, you can figure this out without writing a single line of code :-)
In their "solution", they make an assumption about the values in the list that may not always hold. Can you figure out what this assumption is?
def getTest():
return [-1]*100
This will make sure it can't find the max value and it will also return 0.
I have a work to do on numerical analysis that consists on implementing algorithms for root-finding problems. Among them are the Newton method which calculates values for a function f(x) and it's first derivative on each iteraction.
For that method I need a way to the user of my application enter a (mathematical) function and save it as a variable and use that information to give values of that function on different points. I know just the very basic of Python programming and maybe this is pretty easy, but how can I do it?
If you trust the user, you could input a string, the complete function expression as it would be done in Python, then call eval() on that string. Python itself evaluates the expression. However, the user could use that string to do many things in your program, many of them very nasty such as taking over your computer or deleting files.
If you do not trust the user, you have much more work to do. You could program a "function builder", much like the equation editor in Microsoft Word and similar programs. If you "know just the very basic of Python programming" this is beyond you. You might be able to use a search engine to find one for Python.
One more possibility is to write your own evaluator. That would also be beyond you, and you also might be able to find one you can use.
If you need more detail, show some more work of your own then ask.
I need a module or strategy for detecting that a piece of data is written in a programming language, not syntax highlighting where the user specifically chooses a syntax to highlight. My question has two levels, I would greatly appreciate any help, so:
Is there any package in python that receives a string(piece of data) and returns if it belongs to any programming language syntax ?
I don't necessarily need to recognize the syntax, but know if the string is source code or not at all.
Any clues are deeply appreciated.
Maybe you can use existing multi-language syntax highlighters. Many of them can detect language a file is written in.
You could have a look at methods around baysian filtering.
My answer somewhat depends on the amount of code you're going to be given. If you're going to be given 30+ lines of code, it should be fairly easy to identify some unique features of each language that are fairly common. For example, tell the program that if anything matches an expression like from * import * then it's Python (I'm not 100% sure that phrasing is unique to Python, but you get the gist). Other things you could look at that are usually slightly different would be class definition (i.e. Python always starts with 'class', C will start with a definition of the return so you could check to see if there is a line that starts with a data type and has the formatting of a method declaration), conditionals are usually formatted slightly differently, etc, etc. If you wanted to make it more accurate, you could introduce some sort of weighting system, features that are more unique and less likely to be the result of a mismatched regexp get a higher weight, things that are commonly mismatched get a lower weight for the language, and just calculate which language has the highest composite score at the end. You could also define features that you feel are 100% unique, and tell it that as soon as it hits one of those, to stop parsing because it knows the answer (things like the shebang line).
This would, of course, involve you knowing enough about the languages you want to identify to find unique features to look for, or being able to find people that do know unique structures that would help.
If you're given less than 30 or so lines of code, your answers from parsing like that are going to be far less accurate, in that case the easiest best way to do it would probably be to take an appliance similar to Travis, and just run the code in each language (in a VM of course). If the code runs successfully in a language, you have your answer. If not, you would need a list of errors that are "acceptable" (as in they are errors in the way the code was written, not in the interpreter). It's not a great solution, but at some point your code sample will just be too short to give an accurate answer.
The functions min and max are very flexible; they can take any number of parameters, or a single parameter that is an iterable. any and all are similar in taking an iterable of any size, but they do not take more than one parameter. Is there a reason for this difference in behavior?
I realize that the question might seem unanswerable, but the process of enhancing Python is pretty open; many seemingly arbitrary design decisions are part of the public record. I've seen similar questions answered in the past, and I'm hoping this one can be as well.
Inspired by this question: Is there a builtin function version of and and/or or in Python?
A lot of the features in Python are suggested based on how much users need them, however they must also conform to the style of the language. People often need to do this:
max_val = 0
for x in seq:
# ... do complex calculations
max_val = max(max_val, result)
which warrants the use of the multiple parameters. It also looks good. I haven't heard of anyone needing to use any(x, y, z) because it is most often used on sequences. For a small number of values you can just use the and/or logical operators and for a lot of values you really should be using a list anyway or your code gets messy. I'm certain that not much thought has gone into this because it really wouldn't benefit anyone, it hasn't been under large demand so the Python devs don't worry about it.