Using Pure Functions as Coordinates in Wolfram Mathematica - dynamic

Does anybody know how to use pure functions in Locator in Wolfram Mathematica? For example, if I had a simple piece of code like this:
Graphics[
Locator[
Dynamic[fill, (fill = #) &]
]
]
There is an error because # is a stand in for a single number, not a pair of coordinates like is needed for Locator. I also tried this:
Graphics[
Locator[
Dynamic[fill, (fill = {#1, #2}) &]
]
]
Which yielded the same issue.

This works fine for me
fill = {1, 1};
{Graphics[Locator[Dynamic[fill, (fill = #) &]]], Dynamic[fill]}

You do not need to specify tracking functions as Wolfram Language will automatically update the variable for you. However, for this particular case you do need to initialise your fill variable.
Try
fill = {0, 0};
{Graphics[Locator[Dynamic[fill]]], Dynamic[fill]}
Hope this helps.

Related

How to solve systems with TI-nspire automatically

is there any solution to solve a large system of equations in a program with the TI-nspire without typing all the variable names by hand?
For example, the function solve([1 2; 3 4]* [x; y] = 2,{x,y}) requires typing x,y manually. How is this done if the dimensions of the matrix can change?
I tried to use the function constructMat(x[i], i, j, 5, 1) which partly works but solve doesn't accept this function as variable as second argument.
Thanks for the help!!
I had this problem a while ago, i couldnt find any solution but to manually type the list of variables. Ive found the way to construct the list of unkowns (B_n):
i:=3:j:=4:B_n:=newlist(j+1) For i1,i,i+j:B_n[i1-i+1]:=expr("b"&string(i1)):endfor
with result: {b3,b4,b5,b6,b7}.
I dont remember exactly why this didnt work, i guess its the same reason (solve or zeros function will not accept this list as an argument)

WolframAlpha, PlotRange function is not working

I'm trying to set the range of the y-axis of a plot in WolframAlpha with PlotRange, but it's not working.
Following the examples, if I run (in the input bar in WolframAlpha):
Plot[Tan[x], {x, 0, 10}, PlotRange -> Automatic]
The result is:
Wolfram|Alpha doesn't understand your query
Why is that? Does this work for you?
Do you know how to solve it, or eventually how can I set the range of the y-axis?
Thanks
Wolfram|Alpha doesn't quite use strict Mathematica / Wolfram Language syntax.
Try this,
plot tan(x), x=-0..10, y=-2..2

What is the lambda function doing in the info_dict parameter of the summary_col in this code?

I'm running summary statistics for a group of standard OLS regressions. The code was written by my professor and I'm trying to figure out what's going on specifically in a portion of the code.
summary_col(
[reg0,reg1,reg2,reg3],
stars=True,
float_format='%0.2f',
info_dict = {
'N':lambda x: "{0:d}".format(int(x.nobs)),
'R2':lambda x: "{:.2f}".format(x.rsquared)
})
I looked up lambda functions. I have a fairly decent understanding of how they work. Aspects of the code that I do understand:
info_dict is a dictionary of values that can be called if you wish to include them in your summary statistics
lambda function work by calling an anonymous function "lambda x" then you place the : and list what operation you want to take place (i.e. x + 5) and then if you already know what parameters you want it to run you can put in a list after a second ":".
{0:d} will round to integers which makes perfect sense for observations. Although I don't know why you can't just say {%.f}. Maybe it's because the former returns an explicit int and the latter returns a float that looks like an int.
{:.2f} will return a float with 2 decimal places
What I don't fully understand is what somestring.format() does. Somehow x is getting defined as the results from the regression I believe and x.nobs is the variable "number of observations". Similar for x.rsquared.
Could someone fill in the gaps for me about what's going on in the formula? What exactly about the lambda function is enabling it to fetch data for each individual regression?
Let's break this out a little bit to make it obvious what is happening:
summary_col(
[reg0,reg1,reg2,reg3],
stars=True,
float_format='%0.2f',
info_dict={
'N':lambda x: "{0:d}".format(int(x.nobs)),
'R2':lambda x: "{:.2f}".format(x.rsquared)
}
)
The summary_col object is taking in some input, the first argument being a list of regression objects, [reg0,reg1,reg2,reg3]. Then there are three named arguments, stars, float_format, and info_dict. When we pass in the list of regression objects as the first argument, I believe that the lambda function knows to apply the anonymous function to each object. So all info_dict is doing is creating a dictionary with two keys, N and R2 which map to strings. When the member x.nobs and x.rsquared are referenced in the lambda functions they are applied against the regression objects due to the context in which these are used.
If you try to use lambda in that line of code on something that does not exist in the regression objects, you'll almost certainly get an error. The key is in the context against which the lambda is applied.
A good example on the context of lambda functions is iterating over a dictionary and sorting by key and value.
# sort the dict by value first, and key second...
# x is inferred from the context (my_dict.items())
for key, value in sorted(my_dict.items(), key=lambda x: (x[1], x[0]):
print(key, value)

How can I change column data type from float to string in Julia?

I am trying to get a column in a dataframe form float to string. I have tried
df = readtable("data.csv", coltypes = {String, String, String, String, String, Float64, Float64, String});
but I got complained
syntax: { } vector syntax is discontinued
I also have tried
dfB[:serial] = string(dfB[:serial])
but it didn't work either. So, I'd like to know what would be the proper approach to change column data type in Julia.
thx
On your first attempt, Julia tells you what the problem is - you can't make a vector with {}, you need to use []. Also, the name of the keyword argument should be eltypes rather than coltypes.
On the second try, you don't have a float, you have a Vector of floats. So to change the type you need to change the type of all elements. In Julia, elementwise operations on vectors are generalized by the 'dot' syntax, e.g. string.(collect(dfB[:serial])) . The collect is needed currently to cast the DataArray to a normal Array first – this will fail if the DataArray contains NAs. IMHO the DataFrames interface is still rather wonky, so expect a few headaches like this ATM.

What is the syntax to instantiate a structured dtype in numpy?

If I have a dtype like
foo = dtype([('chrom1', '<f4', (100,)), ('chrom2', '<f4', (13,))])
How can I create an instance of that dtype, as a scalar.
Background, in case There's A Better Way:
I want to efficiently represent arrays of scalars mapping directly to the bases in a genome, chromosome by chromosome. I don't want arrays of these genomic arrays, each one is simply a structured set of scalars that I want to reference by name/position, and be able to add/subtract/etc.
It appears that dtype.type() is maybe the path forward, but I haven't found useful documentation for correctly calling this function yet.
So suppose I have:
chrom1_array = numpy.arange(100)
chrom2_array = numpy.arange(13)
genomic_array = foo.type([chrom1_array, chrom2_array])
That last line isn't right, but hopefully it conveys what I'm currently attempting.
Is this a horrible idea? If so, what's the right idea? If not, what's the correct way to implement it?
This sort of works, but is terrible:
bar = np.zeros(1, dtype=[('chrom1', 'f4', 100), ('chrom2', 'f4', 13)])[0]
try this:
foo = np.dtype([('chrom1', '<f4', (100,)), ('chrom2', '<f4', (13,))])
t = np.zeros((), dtype=foo)