Is it possible to compare two vectors in GAMS directly? For example,
set i 'plants' /i0*i9/
parameter a(i),b(i);
a(i)=uniformint(1,9);
b(i)=uniformint(1,9);
if(a(i)=b(i),
display a;
else
display b;
);
Directly using this code will result in errors. However, is it possible to use comparison of two vectors in my if condition?
Thanks, Sophia
While your conditional expression looks right, there is a loop over i missing.
This could look like the following:
Loop (i,
if(a(i)=b(i),
display a;
else
display b;
);
);
Related
I have been developing a Scilab function where I need to have persistent variable of the matrix type. Based on my similar question I have chosen the same approach. Below is the code I have used for test of this approach.
function [u] = FuncXYZ(x)
global A;
global init;
if init == 0 then
init = 1;
A = eye(4, 4);
endif
u = A(1, 1);
endfunction
As soon as I have integrated the function inside my Xcos simulation I have been surprised that I see "0" at the output of the scifunc_block_m.
Nevertheless I have found that in case I use below given command for "return" from the function
u = A(3, 3);
the function returns really the expected "1". Additionaly if I take a look at the Variable Browser on the top right corner of the Scilab window I can't se the expected A 4x4 item. It seems that I am doing something wrong.
Can anybody give me an advice how to define a persistent variable of the matrix type inside the Scilab function?
Thanks in advance for any ideas.
Global variables are by default initialized with an empty matrix. Hence, you should detect first call with isempty()
function [u] = FuncXYZ(x)
global A;
global init;
if isempty(init)
init = 1;
A = eye(4, 4);
end
u = A(1, 1);
endfunction
BTW, your code is incorrect, there is no endif in Scilab.
This question is about the GAP and the GAP programming language:
I would like to write code that will compute the following formula:
(n^+1)/GcdInt(n^2+1, 2^(Tau(n^2+1)));
for the first 100,000 integers n.
Here is what I have so far:
f:=function(n);
f:=(n^+1)/GcdInt(n^2+1, 2^(Tau(n^2+1)));
return f;
end;
It is grossly wrong. I cannot find any good sources on writing GAP code that I can understand unfortunately.
I got it:
for i in [1..100000] do
Print((i^2+1)/GcdInt(i^2+1,2^Tau(i^2+1)), " ");
od;
Print( "\n" );
I have an array
int lc[2][2]=
{
{HIGH,LOW},
{LOW,HIGH}
};
and I want to create a for loop with
for(lc[2][2]==HIGH){...}
but it only works for
for(lc[1][1]==HIGH){...}
I don't understand what I'm doing wrong. Thank you!
Your two-dimensional array consist of two rows and two columns.
You can access the values with lc[0][0] and lc[1][1].
You have not assigned the index 2 a value, so you would not get into the loop.
Below is the code for my program. I'm attempting to find the value of the integral of 1/ln(x), and then evaluate the integral from 0 to x, with this as the integrand. I'm not exactly sure what I'm doing wrong, but I am quite new to Scilab.
t = input("t");
x=10; while x<t, x=x+10,
function y=f(x), y=(1/(log (x))), endfunction
I=intg(2,x,f);
function z=g(x), z=I, endfunction
W = intg(0,x,z);
W
end
I'm not entirely sure on what you are trying to achieve, but I reformatted your code and added some suggestions to documentation.
Maybe it will help you in finding the answer.
While loop
You can convert your while loop to a for loop
Your code
x=10;
while x<t
x=x+10
//some code
end
Could be
for x=10:10:t
//some code
end
Functions
In your code, you redeclare the two functions every single iteration of the while loop. You could declare them outside the while loop and call them inside the loop.
Reformatted
t = input("Please provide t: ");
// The function of 1/ln(x)
function y=f(x), y=1/log(x), endfunction
// Every time g(x) is called the current value of I is returned
function z=g(x), z=I, endfunction
for x=10:10:t
//Find definite integral of function f from 2 to x
I = intg(2,x,f);
//Find definite integral of I from 0 to x
W = intg(0,x,g);
disp( string(W) );
end
I know the question is porbably outdated; but the topic is still active. And I was looking for a code with double integral.
Here, it looks strange to use "intg" just to calculate the area of the rectangle defined by its diagonal ((0,0), (x,I)): the result is just x*I...
May be the initial aim was to consider "I" as a function of "x" (but in this case there is a convergence problem at x=1...); so restricting the integration of "I" to something above 1 gives the following code:
x=10:10:100;W2=integrate('integrate(''1/log(x2)'',''x2'',2,x1)','x1',1.001,x);
Note the use of integration variables x1 and x2, plus the use of quotes...
So instead of writing a looping function where you instantiate an array and then set each index value as the index, is there a way to do this in LINQ?
Enumerable.Range(0, 10) will give you an IEnumerable<int> containing zero to 9.
You can use the System.Linq.Enumerable.Range method for this purpose.
Generates a sequence of integral numbers within a specified range.
For example:
var zeroToNineArray = Enumerable.Range(0, 10).ToArray();
will create an array of sequential integers with values in the inclusive range [0, 9].
You might want to look at Enumberable.Range
For Each( var i in Enumberable.Range(1,5).ToArray()){
Console.WriteLine(i)
}
would print out 1,2,3,4,5