We have a UI for users to put down LaTex responses. To evaluate those responses, we translate LaTex to R, then run some script similar to the following to compare the response to the correct answer.
x = #{random number}
y = #{random number}
abs(x*x*y - x^2*y) < 10^-6
It's working fine for those functions mapped well. For example:
\frac{x}{y} => x/y
\left|x\right| => abs(x)
{\cdot} => *
But I didn't find any match for the following functions/notations:
\hat{x} -- unit vector
\vec{x} -- vector
\sqrt[3]{x} -- nth root
Any suggestions?
PS:
There is a hat function in R, but I'm not sure if it's the same as the one in LaTex.
Also, \sqrt[3]{x} can be replaced by x^(1/3). Will this be always working? Any performance concern? Someone's saying x^(1/2) is slower than sqrt(x).
Related
I am having a problem running a spiking-neuron simulator. I keep getting the error message, "operation +: Warning adding a matrix with the empty matrix will give an empty matrix result." Now I'm writing this program in "Scilab," but I'm hoping the problem I am having will be clear for the educated eye regardess. What I am doing is converting an existing MATLAB program to Scilab. The original MATLAB program and an explanation can be found here: https://www.izhikevich.org/publications/spikes.pdf
What happens in my Scilab version is that the first pass through the loop produces all the expected values. I Know this becuase I hit pause at the end of the first run, right before "end," and check all the values and matrix elements. However, if I run the program proper, which includes a loop of 20 iterations, I get the error message above, and all of the matrix values are empty! I cannot figure out what the problem is. I am fairly new to programming so the answer may be very simple as far as I know. Here is the Scilab version of the program:
Ne=8; Ni=2;
re=rand(Ne,1); ri=rand(Ni,1);
a=[0.02*ones(Ne,1); 0.02+0.08*ri];
b=[0.2*ones(Ne,1); 0.25-0.05*ri];
c=[-65+15*re.^2; -65*ones(Ni,1)];
d=[8-6*re.^2; 2*ones(Ni,1)];
S=[0.5*rand(Ne+Ni,Ne), -rand(Ne+Ni,Ni)];
v=60*rand(10,1)
v2=v
u=b.*v;
firings=[];
for t=1:20
I=[5*rand(Ne,1,"normal");2*rand(Ni,1,"normal")];
fired=find(v>=30);
j = length(fired);
h = t*ones(j,1);
k=[h,fired'];
firings=[firings;k];
v(fired)=c(fired);
u(fired)=u(fired)+d(fired);
I=I+sum(S(:,fired),"c");
v=v+0.5*(0.04*v.^2+5*v+140-u+I);
v=v+0.5*(0.04*v.^2+5*v+140-u+I);
u=u+a.*(b.*v-u);
end
plot(firings(:,1), firings(:,2),".");
I tried everything to no avail. The program should run through 20 iterations and produce a "raster plot" of dots representing the fired neurons at each of the 20 time steps.
You can add the following line
oldEmptyBehaviour("on")
at the beginning of your script in order to prevent the default Scilab rule (any algebraic operation with an empty matrix yields an empty matrix). However you will still have some warnings (despite the result will be OK). As a definitive fix I recommend testing the emptyness of fired in your code, like this:
Ne=8; Ni=2;
re=rand(Ne,1); ri=rand(Ni,1);
a=[0.02*ones(Ne,1); 0.02+0.08*ri];
b=[0.2*ones(Ne,1); 0.25-0.05*ri];
c=[-65+15*re.^2; -65*ones(Ni,1)];
d=[8-6*re.^2; 2*ones(Ni,1)];
S=[0.5*rand(Ne+Ni,Ne), -rand(Ne+Ni,Ni)];
v=60*rand(10,1)
v2=v
u=b.*v;
firings=[];
for t=1:20
I=[5*rand(Ne,1,"normal");2*rand(Ni,1,"normal")];
fired=find(v>=30);
if ~isempty(fired)
j = length(fired);
h = t*ones(j,1);
k=[h,fired'];
firings=[firings;k];
v(fired)=c(fired);
u(fired)=u(fired)+d(fired);
I=I+sum(S(:,fired),"c");
end
v=v+0.5*(0.04*v.^2+5*v+140-u+I);
v=v+0.5*(0.04*v.^2+5*v+140-u+I);
u=u+a.*(b.*v-u);
end
plot(firings(:,1), firings(:,2),".");
The [] + 1 is not really defined in a mathematical sense. The operation might fail or produce different results depending on the software you use. For example:
Scilab 5 [] + 1 produces 1
Scilab 6 [] + 1 produces [] and a warning
Julia 1.8 [] .+ 1 produces [] but [] + 1 an error.
Python+Numpy 1.23 np.zeros((0,0)) + 1 produces [].
I suggest checking with size() or a comparison to the empty matrix to avoid such strange behaviour.
update:
I've isolated the issue of why the legend() wasn't working. The question regarding that:
Does `legend ( )` require implicit variables which get lost in different scope
I am trying to call a variable in the format ";displayname;" property for a legend in plot 15.2.1 2-D plots, but I can't find any documentation regarding it.
I can't get the legend() to work probably due to the way my actual code is structured. And I am unable to debug as the Octave GUI is not working on my pc & I don't know how to debug on the Octave CLI.
so, defining legend within the plot() somehow is the only resort as it seems to me.
Isolated test codes:
cur = char ( ["hi";"he";"le"] )
% Legend works in isolation, but not in my actual program
loglog ( 3*[1:10].^3 )
legend ( cur(1,:) )
% Tries:
% Prints verbatim
loglog ( 3*[1:10].^3, ";cur(1,:);" )
% Throws syntax error
loglog ( 3*[1:10].^3, ";"cur(1,:)";" )
% Throws error "__plt2vv__: vector lengths must match"
loglog ( 3*[1:10].^3, (";" + cur(1,:) + ";") )
The second & third tries were based on this post on MATLAB central
You can use sprintf() to explicitly build strings. Use %s to denote a string variable to add: sprintf(";%s;", cur(1, :)).
Using array concatenation you could do [";", cur(1, :), ";"]. Octave doesn't do string concatenation using the + operator as far as I know.
When I write the following:
tempx <- tempx (-1, c(-4, -2:-20))
I get the following error message:
Could not find function "tempx"
But when I write the following the code runs properly:
tempx <- tempx [-1, c(-4, -2:-20)]
Please let me know the diff between () & [].
() is used to call a function. [] is used for subsetting vectors, arrays and matrices (and other such objects).
I'd suggest if you haven't already, reading an introduction to R, also available by typing help.start() into R itself. In particular, you might like to look at sections 2.1 Vectors and assignment and 5.2 Array indexing. Subsections of an array.
I am trying to display both sides of a vector equation.
I am able to display() the rhs of the equation but I am unable to define an equality sympy.Eq(symbols("F_T"), F_Tx*N.x + F_Ty*N.y + F_Tz*N.z) using vectors. Ideally I would like F_T to its own symbol (for use in other equations) but creating a vector modifies the type() from "symbol" to "Vector".
Therefore I am unable to print the desired full equation (like a textbook might). I am open to printing in LaTeX but I am uncertain how to combine my sympy expressions with created LaTeX. Any help is appreciated, Thanks!
I would like to be able to pretty-print: F_T = F_Tx*N.x + F_Ty*N.y + F_Tz*N.z
I've been wondering about how to do the same thing. The following tips are not a perfect solution, but they may prove useful to someone.
def create_vector(sym, refFrame):
unit_vectors = refFrame.base_vectors()
syms = sp.symbols(' '.join([sym + '_{' + str(ax) + '}' for ax in unit_vectors]))
vector = reduce(add, [sym * unit for sym, unit in zip(syms, unit_vectors)])
return (vector,) + syms
This function allows you to quickly create a vector with unresolved symbolic components, like so:
XYZ = CoordSys3D('XYZ', vector_names=('I', 'J', 'K'))
r_rel, r_rel_i, r_rel_j, r_Rel_k = create_vector('\mathbf{r_{rel}}', XYZ)
r_rel
Now, if you try to render this in Jupyter, you get the ugly component representation:
But, if you are prepared to be a bit hacky, you can work around this. The Zero vector stores it's own latex representation internally, as self._latex_form. The sympy latex
printer determines if a vector is the zero vector by checking if expr == expr.zero.
We can abuse these and trick the latex printer into printing a nicer representation:
r_rel.zero = r_rel
r_rel._latex_form = '\mathbf{r_{rel}}'
r_rel
For which we get this prettier representation:
Admittedly, this could break things, and it will make you feel dirty. But it may help someone.
Try creating an equality to display: eq = Eq(F_T, F_Tx*N.x ...). An equality is printed with an equals sign separating the lhs and rhs of the equality.
Problem
I read in an array of strings from a file.
julia> file = open("word-pairs.txt");
julia> lines = readlines(file);
But Julia doesn't know that they're strings.
julia> typeof(lines)
Array{Any,1}
Question
Can I tell Julia this somehow?
Is it possible to insert type information onto a computed result?
It would be helpful to know the context where this is an issue, because there might be a better way to express what you need - or there could be a subtle bug somewhere.
Can I tell Julia this somehow?
No, because the readlines function explicitly creates an Any array (a = {}): https://github.com/JuliaLang/julia/blob/master/base/io.jl#L230
Is it possible to insert type information onto a computed result?
You can convert the array:
r = convert(Array{ASCIIString,1}, w)
Or, create your own readstrings function based on the link above, but using ASCIIString[] for the collection array instead of {}.
Isaiah is right about the limits of readlines. More generally, often you can say
n = length(A)::Int
when generic type inference fails but you can guarantee the type in your particular case.
As of 0.3.4:
julia> typeof(lines)
Array{Union(ASCIIString,UTF8String),1}
I just wanted to warn against:
convert(Array{ASCIIString,1}, lines)
that can fail (for non-ASCII) while I guess, in this case nothing needs to be done, this should work:
convert(Array{UTF8String,1}, lines)