Is there a reciprocal function in Octave? - octave

Is there a built-in function in Octave to compute the element-wise reciprocal of a matrix, or is it necessary to implement a function that runs code like mRecip = ones(size(m)) ./ m?
A quick search through the documentation for "recip" or "reciprocal" didn't yield anything useful.

The following seems to work just as well:
mRecip = 1./m

Related

CUDA tridiagonal solver function (cusparse)

In my CUDA code I am using cusparse<t>gtsv() function (more precisely, cusparseZgtsv and cusparseZgtsvStridedBatch ones).
In the documentaion it is said, that this function solves the equation A*x=alpha *B. My question is - what is alpha? I didn't find it as an input parameter. I have no idea how to specify it. Is it always equals to 1?
I performed some testing (solved some random systems of equations where tridiagonal matrices were always diagonally dominant and checked my solution using direct matrix by vector multiplication).
It looks like in the current version alpha = 1 always, so one can just ignore it. I suspect that it will be added as an input parameter in future releases.

Theano: Explicit transfers of data between GPU dosen't work

I'm trying to implement a simple sample to show how to calculate two theano.tensor.dot in two different GPUs. Where the two dot shares the same A and different B.
theano.tensor.dot(A,B0); theano.tensor.dot(A,B1)
I'm willing to store the B0 and B1 in different GPUs. And A was originally stored in one GPU, and then I made a copy to another GPU with explicit transfer function. Finally, I dot separately in the two GPUs.
My implementation is as below:
import numpy
import theano
va0 = theano.shared(numpy.random.random((1024, 1024)).astype('float32'),
target='dev0')
va1 = va0.transfer('dev1')
vb0 = theano.shared(numpy.random.random((1024, 512)).astype('float32'),
target='dev0')
vb1 = theano.shared(numpy.random.random((1024, 2048)).astype('float32'),
target='dev1')
vc0 = theano.tensor.dot(va0,vb0)
vc1 = theano.tensor.dot(va1,vb1)
f = theano.function([], [vc1,vc0])
print f()
While I was looking into the nvprof result, I found that the two dot still run in the same GPU. And the va0.tranfer('dev1') doesn't work. Actually it copied vb1 into dev0 instead, and both the dots computed in dev0.
I tried sever combinations of Theano Flags but doesn't work. Any one can help?
nvprof: two dot in same gpu
The Theano Flag below solves the issue.
export
THEANO_FLAGS="contexts=dev0->cuda0;dev1->cuda1,optimizer_verbose=True,optimizer_excluding=local_cut_gpua_host_gpua"
optimizer_verbose provides the optimization done by theano function. I notice one line like below:
local_cut_gpu_transfers HostFromGpu(gpuarray).0
HostFromGpu(gpuarray).0
where local_cut_gpu_transfers is the reason
HostFromGpu(gpuarray).0 is original node the last segment is what the original node be replaced to.
Then, I searched the keyword "local_cut_gpu_transfer" in source code of Theano, until I found:
optdb['canonicalize'].register('local_cut_gpua_host_gpua',
local_cut_gpu_transfers,
'fast_compile', 'fast_run', 'gpuarray')
Then I add 'local_cut_gpua_host_gpua'to optimizer_excluding in Theano Flag.
Hopes that Theano will provide a detailed map of the reason and the optimizer Theano Flag?

Writing a simple user-defined function in Maple

I'm a total novice in Maple (I usually use R) but I'm trying to do some analytic transformations on a user-defined function, hence my choice of Maple.
The function is in t and I have a vector of known values (basically, known constants) (t_1,..., t_n) which is passed to the function and I also know the values for alpha, beta and miu.
I cannot figure out how to code the summation of exponentials in Maple: in R I would simply use sum().
I have looked at the examples in http://www.maplesoft.com/support/help/maple/view.aspx?path=sum but none seems to apply to my case. I don't want to evaluate the function but I want to be able to find, analytically, the integral or the derivative of it (or of functions of it.)
Links to manuals or web-pages that deal with similar problems/cases are very much welcome!
Make the vector of constants global, not passed in. I'll call it T. Then the function is
lambda:= t-> mu+alpha*add(exp(-beta*(t-tau)), tau= T);

matlab function which is a function of an intergral

I need to write my own function which has the form f(x,y)=Integrate(g(x,y,z),z from 0 to inf). so the code I used was:
function y=f(x,y)
g=#(z)exp(-z.^2)./(z.^x).*(z.^2+y.^2).^(x/2);% as a function of x,y and z
y=quadgk(g,0,inf)
and if I call it for a single value like f(x0,y0), it works but if I try to calculate something like f([1:10],y0), then the error message says that there is something wrong with the times and dimension. In principle I can use for loops but then my code slows down and takes forever. Is there any help I can get from you guys? or references?
I'm trying to avoid the for loop since in matlab it's much faster to use matrix computation than to use for loop. I wonder if there is any trick that I can take advantage of this feature.
Thanks for any help in advance,
Lynn
Perhaps you can try to transpose the interval, creating row based values instead of column based f([1:10]',y0). Otherwise something in your function might be wrong, for example to get x^y to work with lists as input, you have to prefix with a dot x.^y. The same for mulitply and division I think..
If loop is no problem for you, you should do something like:
function y2=f(x,y)
y2=zeros(size(x));
for n=1:numel(x)
g=#(z)exp(-z.^2)./(z.^x(n)).*(z.^2+y.^2).^(x(n)/2);% as a function of x,y and z
y2(n)=quadgk(g,0,inf)
end
The problem here is that quadk itself uses vectors as argument for g. Then you have in g somethink like z.^x, which is the power of two vectors that is only defined if z and x have the same dimension. But this is not what you want.
I assume that you want to evaluate the function for all arguments in x and that the output vector has the same dimension as x. But this does not seem to be possible since even this simple example
g=#(x)[x;x.^2]
quad(g,0,1)
does not work:
Error using quad (line 79)
The integrand function must return an output vector of the same length as the
input vector.
A similar error shows when using quadgk. The documentation also says that this routine works only for scalar functions and this is not surprising since an adaptive quadrature rule would in general use different points for each function to evaluate the integral.
You have to use quadvinstead, which can integrate vector valued functions. But this gives wrong results since your function is integrated in the interval [0,\infty).

Finding the Maximum

How to find the following Maximum or supremum by computer software such as Mathematica and Matlab: $\sup\frac{(1+s)^{4}+(s+t)^{4}+t^{4}}{1+s^{4}+t^{4}}$?
Instead of numerical approximation, what is the accurate maximum?
Thanks.
Since the question seems a bit like homework, here's an answer that starts a bit like a lecture:
ask yourself what happens to the function as s and t go to small and to large positive and negative values; this will help you to identify the range of values you should be examining; both Mathematica and Matlab can help your figure this out;
draw the graph of your function over the range of values of interest, develop a feel for its shape and try to figure out where it has maxima; for this the Mathematic Plot3D[] function and the Matlab plot() function will both be useful;
since this is a function of 2 variables, you should think about plotting some of its sections, ie hold s (or t) constant, and make a 2D plot of the section function; again, develop some understanding of how the function behaves;
now you should be able to do some kind of search of the s,t values around the maxima of the function and get an acceptably accurate result.
If this is too difficult then you could use the Mathematica function NMaximize[]. I don't think that Matlab has the same functionality for symbolic functions built-in and you'll have to do the computations numerically but the function findmax will help.
In Matlab, one would create a vector/matrix with s and t values, and a corresponding vector with the function values. Then you can pinpoint the maximum using the function max
In Mathematica, use FindMaximum like this:
f[s_,t_]:= ((1+s)^4 + (s+t)^4 + t^4)/(1+s^4+t^4)
FindMaximum[ f[s,t],{s,0},{t,0} ]
This searches for a maximum starting from (s,t)=(0,0).
For more info, see http://reference.wolfram.com/mathematica/ref/FindMaximum.html