Scilab: write an instruction over multiple lines - scilab

Consider a file test.sci containing a single instruction written over two lines
a = 1 + 2
+ 3;
When executing it (File->Execute), Scilab outputs
a = 3
as if the second line had been commented out. In other words, this is what scilab sees:
a = 1 + 2;
// + 3;
Yet, sometimes it is important for the code to remain readable to write an instruction over multiple lines. How to do this with scilab?
PS: I'm using Scilab v5.5.2 .

An instruction can be continued on the next line using two dots ..
a = 1 + 2 ..
+ 3;
The Matlab way i.e. putting an ellipsis ... can also be used
a = 1 + 2 ...
+ 3;

Related

What is non-trivial way of finding solution in systems of linear equations?

Hi!
I think I understood till (2.40) but i don't seem to understand
where the 0 = 8c1 + 2c2 - 1c3 + 0c4 came from.. where did this -1 and 0 is from?
In general these types of questions are probably better suited for https://math.stackexchange.com/ but since I like math and linear algebra I'll also answer your question.
The system of equations that you have has 4 variables and only 2 equations, so this means that you're going to have more than one solution and actually an infinite number of solutions. Not just anything will work though, so let's find what the solutions look like.
To simplify writing this let's call the 2x4 matrix A and the right hand side b=(42,8)T so what we are trying to solve is Ax = b. Also to write the zero vector (0,0)T I'll use ⍬ to save typing.
They first find a particular solution (some xp such that Axp = b) they do this with the first 2 columns and 0's for the other columns: xp = (42,8,0,0)T and when we plug this in we do get Axp = (42,8)T. Next they are trying to find the other solutions.
Notice that if we can find an x0 such that Ax0 = ⍬ then we can add this to our xp and it'll still be a solution: A(xp + x0) = Axp + Ax0 = b + ⍬ = b. So let's see if we can find an x0.
They do this in (2.40) by basically saying the 3rd and 4th column are not used in xp so let's see if we can make them 0 using the first two columns. Really we're looking for anything that'll give Ax0=⍬ and this is just an idea how we might find that.
Now notice that the 3rd column (8,2)T can be written as 8 times first column + 2 times second column. So if we do 8 first column + 2 second column - 1 times third column (+ zero times forth column) we get zero. This is just x0 = (8,2,-1,0)T because if we do Ax0 = A(8,2,-1,0)T = ⍬. Similarly we can find another one of these by using the forth column to get (-4,12,0,-1)T as another independent vector which I'll call x02 because I can't think of a better notation at the moment. Both of these satisfy Ax0 = ⍬, Ax02 = ⍬.
To make this really concrete you can see that computing A(xp+x0) = A(50,10,-1,0)T really does give you (42,8)T.
So we can add either or both to our original xp and it'll still be a solution that gives us the b we're looking for. A(xp + x0 + x02) = Axp + Ax0 + Ax02 = b + ⍬ + ⍬ = b Also any multiple of these x0 or x02 will work as well because for example A(3*x0) = 3*Ax0 = 3*⍬ = ⍬.
So really A(xp + c*x0 + d*x02) = Axp + c*Ax0 + d*Ax02 = b + c*⍬ + d*⍬ = b which means that any vector of the form xp + c*x0 + d*x02 where c and d are any numbers (scalars) will be a solution. This is our solution set and is what the last part (2.43) is saying.

How to detect errors for Reed-Solomon Code?

I am using (7,5) Reed-Solomon Error Correction Code.
I think I can decode "correct 1 error" or "find 2 error position".
However, there is a problem. My code can not find 2 error position.
For example, the message is 1 3 5 2 1 and RS parity is 0 5. So RS code is 0513521.
After then, there are two errors at parity part. So code is changed to 1113521.
I want to find these two errors, but my decoder said the answer is 1113621.
What should I do?
RS(7,5) can correct 1 error or detect up to 2 errors, but not determine the position of the 2 errors. In a two error case, there are multiple combinations of 2 error values and 2 error locations that produce the same 2 syndromes. Using your example, the two error cases 1113521 (errors in locations 0 and 1) and 0463521 (errors in locations 1 and 2) produce the same result: syndrome_1 = 4 and syndrome_2 = 6, and there's no way to determine where the errors are, only that they exist.
As commented, if a 1 error correction is attempted in a 2 error case, it's possible for the decoder to mis-correct and create a third error, in order to create a "valid" codeword, in this case it created 1113621. I got the same result with a test program I have.
The question is missing information, based on the example, it's using GF(8) = GF(2^3), modulo x^3 + x^2 + 1 (hex d), and the generator polynomial = (x-2)(x-4) = x^2 + 6 x + 5. Note for GF(2^m), addition and subtraction are both xor. The data is displayed least significant term first, so 0513521 = 0 + 5x + 1x^2 + 3x^3 + 5x^4 + 2x^5 + 1x^6.

Why would an R function not write to the environment?

I'm trying to write a relatively simple AR(1) representation in R. I cannot find any glaring issues with this code, and furthermore I am returning not errors, it simple isn't writing to the environment, or recognizing areone2 as a function. Any suggestions would be much appreciated.
areone2<-function(y,N,p,d){
yvec<-c(rep(y, times = N))
for(i in 1:N){
yvec[i+1]<-
((1+p*(yvec[i]-d))
+ d)
}
plot(yvec, type='l', xlab="N", ylab="yeild")
}
areone2(.3,10,.9,.2)
It doesn't trigger an error or warning because you broke the line in the middle of a binary operation, but that binary operation wasn't recognized by the parser. It's perfectly legal to begin a line with + 3 It's just 3, which is not what you want.
For example, 2 + 3 is 5, we would expect. But +3 on a new line does not add it to the previous line
> 2 ## break the line here and R returns 2
[1] 2
> +3 ## adding three next is not recognized as a continuation of a call
[1] 3
But you can still break the line if you wrap the call in parentheses (not brackets)
(2
+ 3)
# [1] 5 ## correct
{2
+ 3}
# [1] 3 ## incorrect
Bringing your yvec[]<- assignment call up onto one line is the cleaner and safer way to go.
yvec[i+1] <- ((1+p*(yvec[i]-d)) + d)

Recursion in Scheme and the call-stack

I am a university student studying Racket/Scheme and C as introductory courses for my CS degree.
I have read online that it is generally best practice to use iteration as opposed to recursion in C because recursion is expensive due to saving stack frames onto the callstack etc...
Now in a functional language like Scheme, recursion is used all the time. I know that tail recursion is a huge benefit in Scheme and it is to my understanding that it only requires one stack frame (can anybody clarify this?) no matter how deep the recursion goes.
My question is: what about non-tail recursion? Does each function application get saved on the callstack? If I could get a brief overview of how this works or point me to a resource I would be grateful; I can't seem to find one anywhere that explicitly states this.
Tail call elimination is required by Scheme. Code that isn't tail call recursion will require an additional stack frame.
For a moment let us assume that javascript supports tail call optimization, the second of these function definition will use only 1 stack frame, while the first, on account of the + will require an additional stack frame.
function sum(n) {
if (n === 0)
return n;
return n + sum(n - 1);
}
function sum(n) {
function doSum(total, n) {
if (n === 0)
return total;
return doSum(total + n, n - 1);
}
return doSum(0, n);
}
Many recursive functions can be written for tail call optimization by putting the result of the computation on the stack
Conceptually invocations for the first definition look like this
3 + sum(2)
3 + sum(2) = 3 + 2 + sum(1)
3 + sum(2) = 3 + 2 + sum(1) = 3 + 2 + 1 + sum(0)
3 + sum(2) = 3 + 2 + sum(1) = 3 + 2 + 1 + sum(0) = 3 + 2 + 1 + 0
3 + sum(2) = 3 + 2 + sum(1) = 3 + 2 + 1 + sum(0) = 6
3 + sum(2) = 3 + 2 + sum(1) = 6
3 + sum(2) = 6
6
invocations for the second definition look like this
sum(3, sum(2)) = sum(5, sum(1)) = sum(6, sum(0)) = 6
Yes, a call in a non-tail position needs to add something to the stack so it knows how to resume work when the call returns. (For a more thorough explanation of stacks, tail calls, and non-tail calls, see Steele's paper Debunking the 'Expensive Procedure Call' Myth, or, Procedure Call Implementations Considered Harmful, or, Lambda: The Ultimate GOTO linked from the lambda papers page at readscheme.org.)
But Racket (and many other Schemes, and some other languages) implement "the stack" so that even if you have deep recursion, you won't run out of stack space. In other words, Racket has no stack overflows. One reason for this is that the techniques for supporting deep recursion coincide with the techniques for supporting first class continuations, which the Scheme standard also requires. You can read about them in Implementation Strategies for First-Class Continuations by Clinger et al.

R: annotate within an equation

Is it possible to annotate within an equation in R? For example:
100 /* item 1 */ + 200 /* item 2 */
giving an answer of 300.
I think /* */ is C code. Although, I am not certain 100 /* item 1 */ + 200 /* item 2 */ will run in C.
If I use 100 # item 1 # + 200 # item 2 # in R I get an answer of 100 because everything after the first # is ignored, as I expected.
I suppose I could use:
# item 1 item 2
100 + 200
I was just thinking that when equations become really long and complex taking up several lines it might be nice to annotate within an equation.
The following works and returns 300:
(100 + # item 1
200 ) # item 2
That requires a new line after every annotation and is the closest that I can come to my initial example above containing only one line.
Thanks for any advice.
It's not possible. As noted in the Comments section of the R language manual, the only comment character is #. Everything from the # to the end of the line is ignored (unless the # is quoted in a string).

Resources