I have a Matlab function that I'm calling from a python script:
import matlab.engine
eng = matlab.engine.start_matlab()
t = (1,2,3)
z = eng.tstFnc(t)
print z
The function tstFnc is as follows:
function [ z ] = tstFnc( a, b, c )
z = a + b + c
This does not work, however, as Matlab receives one input instead of three. Could this be made to work?
Note: this is a simplified case of what I want to do. In the actual problem I have a variable number of lists that I pass into a Matlab function, which is interpreted in the Matlab function using varargin.
As notes in the comments, the arguments need to be applied instead of passed as a tuple of length 1.
z = eng.tstFnc(*t)
This causes a call to tstFnc with len(t) arguments instead of a single tuple argument. Similarly you could just pass each argument individually.
z = eng.tstFnc(1, 2, 3)
Related
Just started learning python and came across this
def f(g):
return g(2)
def square(x):
return x ** 2
print(f(square)) # which gives 4
How does inputting square into the f function equate to 4?
When you call a function, the value of the argument is assigned to the named parameter. The call
print(f(square))
can be thought of as "expanding" to
g = square
print(g(2))
Calling f(square) is square(2) which is 2**2 so 4
In python functions are first-class values, considered objects as anything else, hence you can pass them as parameters to another function.
In your case, you are "composing" f with g, which in turn has an invocation with fixed 2 inside f. In this sense, f is a function whose purpose is to feed 2 into another function, given as argument.
Knowing that g squares a number, it's easy to see how g(2) is 4, then f(g) will return 4.
I have defined a function as below:
def Example(M):
......
.....
return A,B
M, A and B are matrices. The function returns A & B. At some point in my code i need only A. How to retrieve only A or B if required. I have tried Example(M).A but it throws error: Tuple object has no attribute A.
The function returns a tuple, which is just a sequence of values with no names.
To reference the n-th value (0-based) in a tuple x, use x[n]
X = Example(M)
A = X[0] # 1st value
However, if you have a function returning a sequence of values, you can use multiple assignment like this:
A, B = Example(M)
You should use A, B = Example(M).
You should also be careful about the indentation of your return statement. It should be indented once to the right.
Title
I don't understand why the following code ignores the first function and run the second instead.
def f(x):
return 100
f = lambda x: 1 if x < 2 else x + f(x-1)
print(f(5))
the output is 15.
Thanks.
You define the first function as f. You immediately over-write that with a different function, calling that one f. This is just as if you'd run the code:
f = [1, 2, 3]
f = 7
print f
You will get only the 7; the list is lost.
The second definition of f overrides the first. Variables can't have two values. And function names are effectively just variables. When you assign to f, you overwrite the value of f, so the original function is no longer accessible.
In Scipy, one-dimensional integration of a function with multiple parameters is achieved by specifying the constant parameters in the function definition through the args argument.
This example is from the Scipy Reference Guide:
>>> from scipy.integrate import quad
>>> def integrand(x, a, b):
... return a * x + b
>>> a = 2
>>> b = 1
>>> I = quad(integrand, 0, 1, args=(a,b))
>>> I = (2.0, 2.220446049250313e-14)
https://docs.scipy.org/doc/scipy/reference/tutorial/integrate.html
I always thought that local variables defined within functions are inaccessible outside the definition. Here, this seems not true since quad only requires integrad as the function argument, and it automatically knows that the variables used are (x, a, b) (and hence (a, b) are taken as parameters in the integration).
What is happening here? What am I missing?
quad doesn't know anything about what variables integrand uses. It doesn't know that the arguments are called a and b. It only sees the values of the global variables a and b, and it passes those values positionally to the integrand. In other words, the code would work the same if you did
x = 2
y = 1
I = quad(integrand, 0, 1, args=(x,y))
quad does not even know how many arguments integrand accepts, other than that it accepts at least one. quad passes the value of the variable of integration as the first argument. You, the user, have to know that, and pass the right number. If you don't (for instance if you didn't pass args, or just passed one arg), you'll get an error. quad just blindly passes along whatever arguments you give it.
I saw this example in a Python book, which showcases how to use a function as an argument to another function:
def diff2(f, x, h=1E-6):
r = (f(x-h) - 2*f(x) + f(x+h))/float(h*h)
return r
def g(t):
return t**(-6)
t = 1.2
d2g = diff2(g, t)
print d2g
My question is, how does this script work without providing an argument to function g? The line in question is:
d2g = diff2(g,t)
Shouldn't it be done like:
d2g = diff2(g(t), t)
g is passed as an argument to diff2. In diff2, that argument is called f, so inside diff2 the name f refers to the function g. When diff2 calls f(x-h) (and the other calls it does), it is calling g, and providing the argument.
In other words, when you do diff2(g, t), you are telling diff2 that g is the function to call. The arguments to g are provided in diff2:
f(x-h) # calls g with x-h as the argument
f(x) # calls g with x as the argument
f(x+h) # calls g with x+h as the argument
If you called diff2(g(t), t), you would be passing the result of g(1.2) as the argument. g would be called before calling diff2, and diff2 would then fail when it tries to call f, because f would be a number (the value g(1.2)) instead of a function.
The functions in question are rather random, and perhaps difficult to understand. Let's consider a simple example, a function sum which takes two numbers a and b, and returns their sum. Actually, we can easily define another function prod, which returns their product too.
def sum(a,b):
return a + b
def prod(a,b):
return a * b
Let's say we have another function compute, which takes as its arguments the operation (a function), and two operands (two numbers to call the function on). In compute, we call the given operation on the arguments.
def compute(fn, a, b):
return fn(a, b)
We can compute different things. We can compute the sum of two numbers:
compute(sum, 1, 3)
We can compute the product of two numbers:
compute(prod, 1, 3)
Basically, without parentheses after the function name, we're not actually calling the function, it's just another object in the namespace (which happens to be a function which we can call). We don't call the function until inside of compute, when we do fn(a,b).
Let's see what the console outputs look like:
>>> compute(sum,1,3)
4
>>> compute(prod,1,3)
3
>>> sum
<function sum at mem-address>
>>> prod
<function prod at mem-address>
>>> sum(1,2)
3