(wx)Maxima plot point by point, numbered - plot

I have a list of roots and I want to plot the real/imaginary parts. If s=allroots(), r=realpart() and i=imagpart(), all with makelist(). Since length(s) can get ...lengthy, is there a way to plot point by point and have them numbered? Actually, the numbering part is what concerns me most. I can simply use points(r,i) and get the job done, but I'd like to know their occurence before and after some sorting algorithms. It's not always necessary to plot all the points, I can plot up until some number, but I do have to be able to see their order of having been sorted out.
I have tried multiplot_mode but it doesn't work:
multiplot_mode(wxt)$
for i:1 thru length(s) do draw2d(points([r[i]],[i[i]]))$
multiplot_mode(none)$
All I get is a single point. Now, if this should work, using draw2d's label(["label",posx,posy]) is very handy, but can I somehow evaluate i in the for loop inside the ""?
Or, is there any other way to do it? With Octave? or Scilab? I'm on Linux, btw.
Just to be clear, here's what I currently do: (I can't post images, here's the link: i.stack.imgur.com/hNYZF.png )
...and here is the wxMaxima code:
ptest:sortd(pp2); length(ptest);
draw2d(proportional_axes=xy,xrange=[sort(realpart(s))[1]-0.1,sort(realpart(s))[length(s)]+0.1],
yrange=[sort(imagpart(s))[1]-0.1,sort(imagpart(s))[length(s)]+0.1],point_type=0,
label(["1",realpart(ptest[1]),imagpart(ptest[1])]),points([realpart(ptest[1])],[imagpart(ptest[1])]),
label(["2",realpart(ptest[2]),imagpart(ptest[2])]),points([realpart(ptest[2])],[imagpart(ptest[2])]),
label(["3",realpart(ptest[3]),imagpart(ptest[3])]),points([realpart(ptest[3])],[imagpart(ptest[3])]),
label(["4",realpart(ptest[4]),imagpart(ptest[4])]),points([realpart(ptest[4])],[imagpart(ptest[4])]),
label(["5",realpart(ptest[5]),imagpart(ptest[5])]),points([realpart(ptest[5])],[imagpart(ptest[5])]),
label(["6",realpart(ptest[6]),imagpart(ptest[6])]),points([realpart(ptest[6])],[imagpart(ptest[6])]),
label(["7",realpart(ptest[7]),imagpart(ptest[7])]),points([realpart(ptest[7])],[imagpart(ptest[7])]),
label(["8",realpart(ptest[8]),imagpart(ptest[8])]),points([realpart(ptest[8])],[imagpart(ptest[8])]),
label(["9",realpart(ptest[9]),imagpart(ptest[9])]),points([realpart(ptest[9])],[imagpart(ptest[9])]),
label(["10",realpart(ptest[10]),imagpart(ptest[10])]),points([realpart(ptest[10])],[imagpart(ptest[10])]),
label(["11",realpart(ptest[11]),imagpart(ptest[11])]),points([realpart(ptest[11])],[imagpart(ptest[11])]),
label(["12",realpart(ptest[12]),imagpart(ptest[12])]),points([realpart(ptest[12])],[imagpart(ptest[12])]),/*
label(["13",realpart(ptest[13]),imagpart(ptest[13])]),points([realpart(ptest[13])],[imagpart(ptest[13])]),
label(["14",realpart(ptest[14]),imagpart(ptest[14])]),points([realpart(ptest[14])],[imagpart(ptest[14])]),*/
color=red,point_type=circle,point_size=3,points_joined=false,points(realpart(pp2),imagpart(pp2)),points_joined=false,
color=black,key="",line_type=dots,nticks=50,polar(1,t,0,2*%pi) )$
This is for 14 zeroes, only. For higher orders it would be very painful.

I gather that the problem is that you want to automatically construct all the points([realpart(...), imagpart(...)]). My advice is to construct the list of points expressions via makelist, then append that list to any other plotting arguments, then apply the plotting function to the appended list. Something like:
my_labels_and_points :
apply (append,
makelist ([label ([sconcat (i), realpart (ptest[i]), imagpart (ptest[i])]),
points ([realpart (ptest[i])], [imagpart (ptest[i])])],
i, 1, length (ptest)));
all_plot_args : append ([proptional_axes=..., ...], my_labels_and_points, [color=..., key=..., ...]);
apply (draw2d, all_plot_args);
The general idea is to build up the list of plotting arguments and then apply the plotting function to that.

Related

Replace for loop with vectorized call of a function returning multiple values

I have the following function: problema_firma_emprestimo(r,w,r_emprestimo,posicao,posicao_banco), where all input are scalars.
This function return three different matrix, using
return demanda_k_emprestimo,demanda_l_emprestimo,lucro_emprestimo
I need to run this function for a series of values of posicao_banco that are stored in a vector.
I'm doing this using a for loop, because I need three separate matrix with each of them storing one of the three outputs of the function, and the first dimension of each matrix corresponds to the index of posicao_banco. My code for this part is:
demanda_k_emprestimo = zeros(num_bancos,na,ny);
demanda_l_emprestimo = similar(demanda_k_emprestimo);
lucro_emprestimo = similar(demanda_k_emprestimo);
for i in eachindex(posicao_bancos)
demanda_k_emprestimo[i,:,:] , demanda_l_emprestimo[i,:,:] , lucro_emprestimo[i,:,:] = problema_firma_emprestimo(r,w,r_emprestimo[i],posicao,posicao_bancos[i]);
end
Is there a fast and clean way of doing this using vectorized functions? Something like problema_firma_emprestimo.(r,w,r_emprestimo[i],posicao,posicao_bancos) ? When I do this, I got a tuple with the result, but I can't find a good way of unpacking the answer.
Thanks!
Unfortunately, it's not easy to use broadcasting here, since then you will end up with output that is an array of tuples, instead of a tuple of arrays. I think a loop is a very good approach, and has no performance penalty compared to broadcasting.
I would suggest, however, that you organize your output array dimensions differently, so that i indexes into the last dimension instead of the first:
for i in eachindex(posicao_bancos)
demanda_k_emprestimo[:, :, i] , ...
end
This is because Julia arrays are column major, and this way the output values are filled into the output arrays in the most efficient way. You could also consider making the output arrays into vectors of matrices, instead of 3D arrays.
On a side note: since you are (or should be) creating an MWE for the sake of the people answering, it would be better if you used shorter and less confusing variable names. In particular for people who don't understand Portuguese (I'm guessing), your variable names are super long, confusing and make the code visually dense. Telling the difference between demanda_k_emprestimo and demanda_l_emprestimo at a glance is hard. The meaning of the variables are not important either, so it's better to just call them A and B or X and Y, and the functions foo or something.

What are the rules for ppp objects? Is selecting two variables possible for an sapply function?

Working with code that describes a poisson cluster process in spatstat. Breaking down each line of code one at a time to understand. Easy to begin.
library(spatstat)
lambda<-100
win<-owin(c(0,1),c(0,1))
n.seeds<-lambda*win$xrange[2]*win$yrange[2]
Once the window is defined I then generate my points using a random generation function
x=runif(min=win$xrange[1],max=win$xrange[2],n=pmax(1,n.seeds))
y=runif(min=win$yrange[1],max=win$yrange[2],n=pmax(1,n.seeds))
This can be plotted straight away I know using the ppp function
seeds<-ppp(x=x,
y=y,
window=win)
plot(seeds)
The next line I add marks to the ppp object, it is apparently describing the angle of rotation of the points, I don't understand how this works right now but that is okay, I will figure out later.
marks<-data.frame(angles=runif(n=pmax(1,n.seeds),min=0,max=2*pi))
seeds1<-ppp(x=x,
y=y,
window=win,
marks=marks)
The first problem I encounter is that an objects called pops, describing the populations of the window, is added to the ppp object. I understand how the values are derived, it is a poisson distribution given the input value mu, which can be any value and the total number of observations equal to points in the window.
seeds2<-ppp(x=x,
y=y,
window=win,
marks=marks,
pops=rpois(lambda=5,n=pmax(1,n.seeds)))
My first question is, how is it possible to add a variable that has no classification in the ppp object? I checked the ppp documentation and there is no mention of pops.
The second question I have is about using double variables, the next line requires an sapply function to define dimensions.
dim1<-pmax(1,sapply(seeds1$marks$pops, FUN=function(x)rpois(n=1,sqrt(x))))
I have never seen the $ function being used twice, and seeds2$marks$pop returns $ operator is invalid for atomic vectors. Could you explain what is going on here?
Many thanks.
That's several questions - please ask one question at a time.
From your post it is not clear whether you are trying to understand someone else's code, or developing code yourself. This makes a difference to the answer.
Just to clarify, this code does not come from inside the spatstat package; it is someone's code using the spatstat package to generate data. There is code in the spatstat package to generate simulated realisations of a Poisson cluster process (which is I think what you want to do), and you could look at the spatstat code for rPoissonCluster to see how it can be done correctly and efficiently.
The code you have shown here has numerous errors. But I will start by answering the two questions in your title.
The rules for creating ppp objects are set out in the help file for ppp. The help says that if the argument window is given, then unmatched arguments ... are ignored. This means that in the line seeds2<-ppp(x=x,y=y,window=win,marks=marks,pops=rpois(lambda=5,n=pmax(1,n.seeds)))
the argument pops will be ignored.
The idiom sapply(seeds1$marks$pops, FUN=f) is perfectly valid syntax in R. If the object seeds1 is a structure or list which has a component named marks, which in turn is a structure or list which has a component named pops, then the idiom seeds1$marks$pops would extract it. This has nothing particularly to do with sapply.
Now turning to errors in the code,
The line n.seeds<-lambda*win$xrange[2]*win$yrange[2] is presumably meant to calculate the expected number of cluster parents (cluster seeds) in the window. This would only work if the window is a rectangle with bottom left corner at the origin (0,0). It would be safer to write n.seeds <- lambda * area(win).
However, the variable n.seeds is used later as it it were the number of cluster parents (cluster seeds). The author has forgotten that the number of seeds is random with a Poisson distribution. So, the more correct calculation would be n.seeds <- rpois(1, lambda * area(win))
However this is still not correct because cluster parents (seed points) outside the window can also generate offspring points inside the window. So, seed points must actually be generated in a larger window obtained by expanding win. The appropriate command used inside spatstat to generate the cluster parents is bigwin <- grow.rectangle(Frame(win), cluster_diameter) ; Parents <- rpoispp(lambda, bigwin)
The author apparently wants to assign two mark values to each parent point: a random angle and a random number pops. The correct way to do this is to make the marks a data frame with two columns, for example marks(seeds1) <- data.frame(angles=runif(n.seeds, max=2*pi), pops=rpois(n.seeds, 5))

The function doesn't return a list and Sage doesn't plot it

I've got the function f(x,y) and I want to iterate it a number of times and plot the resulting points. What I've done is:
def orbita(p,n):
a = [p]
for i in range(n-1):
p = f(p[0],p[1])
a.append(p)
print a
When applied to the point p = (1,2) and asked for n = 5 iterations, this function returns the following:
[(1, 2), (2, 5/2), (5/2, 29/20), (29/20, 1241/1450), (1241/1450, 7285162/5218405)]
Which is correct. However, when I try to plot this list of points by point(orbita((1,2),5)), I simply get an empty plot.
At first I thought that this was an issue with the fact that point() plots "either a single point (as a tuple), a list of points, a single complex number, or a list of complex numbers", quoted from here, because I checked type(orbita((1,2),5)) and got NoneType. However, when I tried to assign the list to a variable by L = orbit((1,2),5) my variable is empty, since typing L returns nothing, so I'm not sure anymore if that is the problem. If I copy the list and write:
point([(1, 2), (2, 5/2), (5/2, 29/20), (29/20, 1241/1450), (1241/1450, 7285162/5218405)])
The plot works perfectly, but I would like to use this function for plotting at least a couple of hundred points, so I wouldn't like to copy them every time. I'm new with both Sage and Python, so I really don't know what I'm doing wrong at any level.
How can I print the outcome of the function orbita(p,n) or how can I modify it so that by typing point(orbita(p,n)) I obtain a plot?
Here is your problem.
this function returns the following:
No, it doesn't. It prints the following. As you correctly identify, it returns None. So you have to have it return the list of tuples. It looks like you are plenty experienced in Python to figure out how to do that, so I won't include the details, but please do follow up if you still have trouble.

How to use different parameters based on a vector for each iteration of lapply?

I have a list of vectors for which I am lapplying the function lines to plot the content of each of the elements in the list. Example code following:
l <- list()
for(i in 1:10){l[[i]] <- rnorm(10)}
plot(l[[1]], t='n')
lapply(l, lines)
Is there a way of telling lapply that for each element use a different parameter, for instance, color or line type, so I can easily attribute the corresponding features I want to each element of the list? For instance, I'd like to have a vector of colors that match a particular element on the list.
I came up with the same lapply approach as jlhoward. Here's an example which colors the line based on whether the average is greater than zero or not:
lapply(l, function(line) {lines(line, col=ifelse(mean(line) > 0, 'red', 'blue'))})
That said, your example uses a loop to create the sample data. If your actual code is using a loop, why not plot each line as part of the data-generating loop? That way you can calculate whatever plotting parameters you need on a per-line basis. Normally, I wouldn't advocate a loop over an apply function -- R is really slow at loops! But unless you are plotting tens of thousands of lines, you probably won't notice much of a performance hit. (Also, bear in mind that the lapply approach is going to return a NULL value for each line plotted ... which is kinda awkward.)
I found that mapply is the way to go

Function return value changes if use local variable

I have two snippets of code which I would have expected to behave the same, but they don't:
position <- function(t) {
coordinates <- c(cosh(t), sinh(t))
return(coordinates[1])
}
and
position <- function(t) {
coordinates <- c(cosh(t), sinh(t))
return(cosh(t))
}
I use the function position to plot a curve. With the first snippet the curve is not plotted. With the second snippet the curve is plotted.
What is the functional difference between the two snippets, and why?
What gets returned will depend on the type of argument passed. If the argument "t" is a matrix as might be expected for a function designed to deal with coordinates, than a matrix is returned from cosh(t) and from sinh(t).
The first function would only return the first element of a matrix formed and then "straightened out" as the c function caused it to loose dimensions. If you wanted to preserve the matrix character, then use rbind or cbind depending on what would be the next function to process the data.
The second function would first calculate "coordinates" and then let it disappear into the garbage collector since it returns the matrix formed by cosh(t) instead.
You will not be able to get a better answer since you are at the moment making us all guess about what sort of data structure you are passing to the function. You should post the results of dput() on your argument to this function. And you should tell us what the help page for the plotting function expects as an argument type.
The result of
coordinates <- c(cosh(t), sinh(t))
is a numeric vector of length 2 * length(t).
The command
return(coordinates[1])
returns only the first value of this vector. (The result of coordinates[1] and cosh(t) are only identical if length(t) == 1.) To return the result of cosh(h), you could index coordinates with a sequence based on the length of t:
coordinates <- c(cosh(t), sinh(t))
return(coordinates[seq_along(t)])
Use double brackets in your first example.
coordinates[[1]]
As a useful tip when troubleshooting, if you explore the output of your two functions using str(position(x)) for your two different functions, you should see the difference.
Try also
str(vec[1])
str(vec[[1]])

Resources