I have a (probably stupid) question:
How can I plot D-lattice data generated by a c++ program in mathematica (9)?
(I'd think intuitively that this can not be too hard but with google, stackoverflow etc. I could not find any solution)
What output format is best for this?
Which mathematica command is best?
At the moment I am trying to give out the data in columns where each row is of the form:
xcoordinate ycoordinate value
I found out that one could in principle solve this by using MatrixPlot[] but for this one would have to provide the data in a matrix form which is incompatible with most other plotting programs so I want to avoid this if possible.
I'm new to StackOverflow and a little bit worried about the requirements for answers. I did my best:
From the facts given in the question, I can't see why not simply use ListPlot3D? You do not need any particular order in the dataset, Mathematica will try to sort your data.
First, you can export your C++ data into a file of the form
x1 y1 value1
x2 y2 value2
x3 y3 value3
...
A file of this format can be imported using
Import["path/to/file.txt", "Table"]
You will then have a matrix like in the following example:
d1 = Flatten[Table[{x, y, Cos[Sqrt[x^2 + y^2]]}, {x, 0, 6, 0.2}, {y, 0, 6, 0.2}], 1];
d2 = RandomSample[d1]
ListPlot3D[d1]
ListPlot3D[d2]
d1 will be a flat list of {x,y,z}
d2 will be the same list in shuffled form
Both will yield the same nice plot of a wave originating in {0,0,1}
As far as I can tell, ListPlot3D is available since Mathematica 8.
Related
In a cell, is it possible to do if(x=y, z, x) without having to repeat x in the value_if_false argument? Whether there is a way of using if() to make this work or another function doesn't matter, and there isn't a specific formula I'm struggling with as I come across this blocker quite often (hence posting).
To help illustrate the need, if we take x as a complex or more advanced formula, such as
ARRAYFORMULA(IF(E$6:Q$6 < EoMONTH($P$4,0), "Not Active", IF(E$6:Q$6<$Q$4 + ISBLANK($Q$4) > 0,
COUNTIF({'Data'!$B$3:$B&'Data'!$I$3:$I&'Data'!$K$3:$K},$B$4&$C9&E$6:Q$6), "Not Active")))
and I wanted to put an if statement in there that changed the result only if a condition was true, the formula would more than double in size due to having to reference x twice:
=ARRAYFORMULA(IF(IF(E$6:Q$6 < EoMONTH($P$4,0), "Not Active", IF(E$6:Q$6<$Q$4 + ISBLANK($Q$4) > 0,
COUNTIF({'Data'!$B$3:$B&'Data'!$I$3:$I&'Data'!$K$3:$K},$B$4&$C9&E$6:Q$6), "Not Active"))) = 0, "No data", IF(E$6:Q$6 < EoMONTH($P$4,0), "Not Active", IF(E$6:Q$6<$Q$4 + ISBLANK($Q$4) > 0,
COUNTIF({'Data'!$B$3:$B&'Data'!$I$3:$I&'Data'!$K$3:$K},$B$4&$C9&E$6:Q$6), "Not Active"))))
This is just an example (the code is irrelevant), I'm trying to keep my formulas neat, tidy and efficient so that handing off to others is easier. Then I'm also mindful that it is calculating the same complex formula twice, which would probably slow the spreadsheet down especially when iterated throughout a spreadsheet.
Interested to hear the community thoughts and suggestions on this, hopefully I was clear in explaining it. :)
The only simple way to achieve this would be with the use of helper columns. They don't need to be in the same sheet as your main equation, but they do need to be within that same spreadsheet as a whole (ie you could have a sheet named "calc" that's specifically used to calculate intermediate steps and set "variables" by referencing those cells).
The only other option (which gets a bit complicated) is to create a custom function within Google Apps Script. For example, if you wanted to calculate (B1*A4)/C5 in multiple places, you could create a custom function like this:
/**
* Returns a calculation using cells A4, B1, and C5.
* #return A calculation using cells A4, B1, and C5.
* #customfunction
*/
function x() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('MainSheet');
var val1 = ss.getRange('B1').getValue();
var val2 = ss.getRange('A4').getValue();
var val3 = ss.getRange('C5').getValue();
return (val1*val2)/val3;
}
Then in your sheet, you could use this within a formula like this:
=if(A1="yes", x(), "no")
This custom function could obviously be altered to fit one's needs (ex taking in arguments to define the cells that the calculations should be done on instead of hard coding them, etc).
Other than this, there is currently no way to define variables within a formula itself.
This is possible to a certain extent, using TEXT's Meta Instructions, if you're using numbers and simple math conditions.
x
y
z
output
10
10
5
10
=TEXT(A3,"[="&B3&"]0;"&C3&"")
x
y
z
output
11
10
5
5
As long as your complex formula returns a number for x(or the output can be coerced to a number), this should be possible and it avoids repetition.
I agree, I would love if there was like a DECODE or NVL type function you could use so that you didn't need to repeat the original statement multiple times.
However, in many cases, when I encounter this, I can often reference another cell. Not in the way that has been suggested already, where the formula exists in another cell, but rather that the decision to perform the formula is based on another cell.
For example, using your values, lets assume the formula ((if(x=y, z, x)) only gets calculated when column 'w' is populated. Maybe column 'w' is a key component of the formula. Then you can write the formula as: if(w="",z,x). It's not exactly the same as testing the answer to the equation first and doesn't work in all situations, but in many cases I can find another field that's of key relevance to the formula that lets me get around this.
I have a cyclical signal I would like to model. I would like to allow the signal to be able to stretch and compress in time, and I do not know the exact profile.
At the moment, I am modelling the phase progression as a random walk, and capturing the cyclical nature by defining the mean likelihood as a sum of sines and cosines on the phase, where the weights on the cosines are parameters to be fitted.
i.e.
y = N(f(phase),sigma) = N(sum_i(a_i*sin(phase) + b_i*cos(phase)),sigma)
(i.e. latex image of above)
This seems to work to some extent, but I would like to change the definition of f so that it does not rely on sums of sin and cos.
I was looking at Gaussian Processes, and thinking that there could be a solution to this there - but I can't figure out how (if it's possible) to define the y in terms of phase when using GP.
There is an example on the pymc github site:
y_obs = pm.gp.GP('y_obs', cov_func=f_cov, sigma=s2_n, observed={'X':X, 'Y':y})
The problem here is that X is defined as observed, while I need to model it as a random variable.
I tried this form:
y_obs = pm.gp.GP('y_obs', X = phase , cov_func=f_cov, sigma=s2_n, observed={ 'Y':y})
But that leads to an error:
File "/home/person/.conda/envs/mcmcx/lib/python3.6/site-packages/pymc3/distributions/distribution.py", line 56, in __init__
raise TypeError("Expected int elements in shape")
I am new to HB/GP/pymc3... and even stackoverflow. Apologies if the question is off.
I'm working with Mathematica,
I begin by importing the data from some CSV files, and I store them under some variable names.
For the moment let's assume we use:
sample1 = Sort[RandomReal[{0, 10}, {10, 2}]];
sample2 = Sort[RandomReal[{0, 10}, {10, 2}]];
sample3 = Sort[RandomReal[{0, 10}, {10, 2}]];
So, I want to do some calculations on the data individually,
I was thinking of using a PopupMenu by "recalling" the defined variables and then I could do a linear fit and display both the data and the linear fit, and also find the highest value:
variables = Names["Global`*"];
PopupMenu[Dynamic[x], variables]
data=Dynamic[x];
lm = LinearModelFit[data, x, x];
Show[ListLinePlot[data, PlotRange-> All],
Plot[lm[x], {x, 0, data[[Length[data], 1]]}]]
Peak = Max[data[[All, 2]]]
But the problem is that when i recall the variables, Mathematica only brings them as names and not the actual data list.
For the moment I'm trying also to store different calculations on a bigger list that would go on storing the values for the calculations:
Data = {};
(This bit would be outside, with the data importing probably, just to define the list)
AppendTo[Data, {Normal[lm], Peak}]
I'm also trying to store the name of the set that's selected, so that's another challenge... Would anyone know how could I solve this issue?
I do realize that it's a pretty specific question (sorry!), but it could be used for other things too! I think...
Thanks!
i am new to machine learning and i wanted to implement multivariate nonlinear regression , and i cant seem to find any good C++ library on it
(for example: data)
y x1 x2 x3 x4 x5 x6
4.52e+005 8.32e+000 4.10e+001 8.801e+002 1.29e+002 3.22e+002 1.26e+002
3.585e+005 8.30e+000 2.10e+001 7.099e+003 1.10e+003 2.40e+003 1.13e+003
3.521e+005 7.25e+000 5.20e+001 1.467e+003 1.91e+002 4.96e+002 1.77e+002
3.413e+005 5.64e+000 5.20e+001 1.274e+003 2.35e+002 5.58e+002 2.19e+002
3.422e+005 3.84e+000 5.20e+001 1.627e+003 2.81e+002 5.65e+002 2.59e+002
i need non linear regression model to predict the output for given input variables(x1,x2,x3,x4,x5,x6)
I would try out kernel ridge regression and/or support vector regression on this. Either of them will probably work quite well.
The dlib C++ library has easy to use implementations of both of these methods. See the support vector regression or kernel ridge regression example programs for the details. Note that these examples show just one input variable but all you need to do is change the dimensionality of the input vector to something other than 1. So in the examples, that means you just change the line
typedef matrix<double,1,1> sample_type;
to
typedef matrix<double,6,1> sample_type;
and then they will work on 6 input variables.
I have to do realtime plotting of scan values of sensor. I am using gnuplot for this purpose. Till now, I am able to communicate to gnuplot from my c++ program. I tried some sample plots using a .DAT file and it is working. Now, My requirement is to plot last 5 values of sensor scan values in a single plot for comparing (that means I need to store 10 arrrays of data. 1 scan have two arrays X and Y).
What I am trying to do is to store the last 5 scan values in a column format in a .DAT file like this where x, y are are my two arrays for each scan.Then using the gnuplot command "plot 'filename.dat' 1:2" "plot 'filename.dat' 2:3" etc... Then I have to rewrite the file after every 5 scans.
X1 Y1 X2 Y2 X3 Y3 X4 Y4 X5 Y5
2.3 3.4 6.6 3.6 5.5 6.5 8.5 5.5 4.5 6.6
4.3 4.5 6.2 7.7 4.3 9.2 1.4 6.9 2.4 7.8
I want to just confirm before proceeding wheather this is efficient for real time processing. Also Is there any command in gnuplot to directly plot from two arrays without the use of .dat files. I did not find one in my search.
Any suggestions would be helpful.
Presumably, you are communicating with gnuplot via pipes. Since gnuplot is a separate process, it does not have access to your programs memory space and therefore it cannot plot your data without you sending it somehow. The most straight forward way is how you mentioned (create a temporary file, send a command to gnuplot to read/plot the temporary file). Another straight forward way is to use gnuplot's inline data...It works like:
plot '-' using ... with ...
x1 y1
x2 y2
x3 y3
...
e
In this case, the datafile is written directly to the gnuplot pipe with no need for a temporary file. (for more questions, about the pseudo-file '-' see help datafile special-filenames in the gnuplot documentation).
As far as this approach being useful in realtime -- as long as the gnuplot rendering speed is fast compared to the time between re-rendering, it should work fine. (I guess there are some memory issues too if your arrays are HUGE, but I doubt that would limit any real application with only 10 1-D arrays -- and if the arrays are that big, you probably shouldn't be sending the whole thing to gnuplot anyway)
Take a look at this: https://github.com/dkogan/feedgnuplot
It's a general-purpose tool to plot standard input. It is able, among other things, to make realtime plots of data, as it comes in. If you have data in a format not directly supported, preprocess your stream with something like awk or perl.