Title pretty much covers it - I need some help understanding a couple of SCILAB idioms (Ultimate goal: convert some SCILAB code into Python)
Until today, I'd never heard of, let alone seen, SCILAB or its code, but while searching for a way to accomplish the goal of my current project, which is mainly written in Python, I've stumbled over a SCILAB module that, based on its description, seems to be pretty darn close to exactly what I'm trying to do - close enough that if I were to translate it into a language I'm actually competent in, it will probably be perfect. Since I'm already working in Python, that will be the translation target.
Most of the SCILAB language looks pretty easy to understand, and I've found docs for it that have already helped, but I've hit a couple of what I assume are SCILAB idioms that are apparently common enough among SCILAB users that nobody/nothing (that I've come across yet, anyway) seems to think they need to be discussed, but make little or no sense to this total n00b to the language.
An example of both of the ones that are giving me trouble can be seen in this function lifted from the module I found:
function [r,theta]=trans_polar(x,y);
r=sqrt(x.*x+y.*y);
theta=atan(y./x);
[nx ny]=size(x);
for i=1:ny
if x(i)>0 then
if y(i)<0 then theta(i) = theta(i)+2*%pi;
end
end
if x(i)<0 then theta(i)=theta(i)+%pi;
end
end
endfunction
It's fairly obvious (or at least, it seems that way to me) that the ultimate goal of this function is to convert a pair of X/Y cartesian coordinates into polar notation, and hand back the converted result as r and theta, but there are a couple of things in the code that have me scratching my head and worrying that I may be overlooking something important while trying to do the translation.
The first is the r=sqrt(x.*x+y.*y)
statement.
Clearly, it's taking the square root of "something" and assigning it to r, but I can't decide precisely what "something" is. If it didn't have those periods/decimal points in it, it would make perfect sense to assume that it's taking the square root of the value (x-squared plus y-squared) and stuffing it into the return value "r". Since it does have them, I'm lost. The next line (theta=atan(y./x)
) gives me the same headache - What do the periods/decimal points mean in both of these statements?
The best guess I can make is that a trailing period MIGHT mean "treat the variable before the period as a floating point value", like a shorthand way of doing a typecast in C - Perhaps equivalent to this C code?
int y = 5;
float x;
x = (float)y;
The other one that has me boggled is the 4th line: [nx ny]=size(x);
Similar statements appear throughout the rest of the code in the module, so it seems to be a pretty common SCILAB idiom, but being common doesn't tell me what it actually means, and at least so far, I can't find anything in the docs I've located that specifically addresses such a construct. I've picked up some hints that make me think that it may be similar in function to the old Applesoft BASIC "DIM" keyword - If you want a two-dimensional array of floating point numbers, 5 rows of 8 columns per row, you'd say "DIM A(5,8)", and A would be a 5x8 array of floating point values, all set to 0.0 - but that's just my best guess.
Am I at least in the right zipcode with this guess? And if not, how should I be reading it?
Any SCILAB users out there who can set me straight?
Both .*
and ./
are element-wise equivalent of multiplication and division operations, since x and y can be vectors of n dimensions, the thing is that this implementation is a more generic form of the transformation from (x, y) as two scalars to polar coordinates.
The [nx ny]=size(x);
line is an assignment statement that is assigning the result of the function size
called with the parameter x
to two variables, nx
and ny
, the same logic goes here, since x can be a vector with n dimensions, it could return multiple values, the documentation of this function says something about that in the Description section.
A possibly simpler way for you to understand this function would be to imagine the simple scenario where both x
and y
are scalars
so .*
would be the same as *
and ./
would be /
and
[nx ny] = size(x)
would assign 1 to both these variables (it would be pointless), so the implementation would be simpler to understand, something like:
function [r, theta] = trans_polar(x, y)
r = sqrt(x*x + y*y);
theta = atan(y / x);
if x > 0 then
if y < 0 then
theta = theta + 2*%pi;
end
end
if x < 0 then
theta = theta + %pi;
end
endfunction
I guess the difficulty you had is mainly because the function you've found works for vectors of any number of dimensions!