// Input area where climax code is expected
/// a1
x=13; y=5*x
z=x**2+y
println x
println "y= "+y

// a2
x=[]
x.add(1.0); x.add(4.5); x<<3.5
println x.size()
x[0]=2.5; x.remove(1)
println x

// a3
x=PI**2
println sqrt(x)

// a4_1
// Initializing a local variable
int a = 2
       
//Check for the boolean condition
if (a<100) {
    //If the condition is true print the following statement
    println("The value is less than 100");
} else {
    //If the condition is false print the following statement
    println("The value is greater than 100");
}

// a4_2
//initializing a local variable
a = 2
       
//Evaluating the expression value
switch(a) {           
    //There is case statement defined for 4 cases
    // Each case statement section has a break condition to exit the loop    
    case 1:
        println("The value of a is One");
        break;
    case 2:
        println("The value of a is Two");
        break;
    default:
        println("The value is neither One or Two");
        break;
}

// a5_1
for(int i = 0;i<5;i++){println(i)}
// or equivalent
for(int i in 0..<5){println(i)}

// a5_2
int i = 0;   
while(i<5) {
    println(i);
    i++;
}

// a5_3
(0..4).each{println(it)}

// a6
double f(double x, double y){
 return Math.sin(2.0*Math.PI*x)*Math.sin(2.0*Math.PI*y)
}
println f(0.85,0.2)

// a7_1
double f(x, y){
 return sin(2.0*PI*x)*sin(2.0*PI*y)
}
println f(0.85,0.2)


// a7_2
f={x, y->
 sin(2.0*PI*x)*sin(2.0*PI*y)
}
println f(0.85,0.2)

// a8_1
//SomeFile = new File("somedirectory/Example.txt")
SomeFile = new File("/home/pchr/Desktop/Example.txt")

SomeFile.write "This is the first line\n"
SomeFile << "This is the second line\n"
SomeFile.leftShift "This is the third line\n"
SomeFile.append "This is the fourth line\n"
println SomeFile.text


// a8_2_1
filetext=SomeFile.text
println filetext

// a8_2_1
lines = SomeFile.readLines()
lines.each{println it}

// a10
order = 3
// define an array double[][]
da=new double[order][order]

for(i in 0..<order){
    for(j in 0..<order){
        da[i][j]=random()
    }
}

// use the above defined double array to equip a matrix M
M=Matrix(da)

// update array da for another use
for(i in 0..<order){
    for(j in 0..<order){
        da[i][j]=random()
    }
}

// use the above defined double array to equip a matrix K
K=Matrix(da)

M.print()
K.print()

(K+M).print()
(K-M).print()
(K*M).print()
M.inverse().print()
M.transpose().print()

// a10_2
Eigen= EigenDescomposition(M.inverse()*K)
EigenValues=Eigen.getRealEigenvalues()
EigenVectors=Eigen.getV()

(1..order).each{println EigenValues[it-1]}
EigenVectors.print()

// a11
c1= new Complex(1.0,2.0)
c2= new Complex(3.0,2.4)
println c1+c2
println c1-c2
println c1*c2
println c1/c2
println c1.conj() // c1.conjugate()
println c1.re() // c1.getReal()
println c1.im() // c1.getImaginary()
println c1.abs() // absolute value of c1

// a12_1
trigPlot = new PlotFrame()
n=100; dt=2.0*PI/n
t=new double[n+1]
s=new double[n+1]
c=new double[n+1]
(0..n).each{
    td=dt*it
    t[it]=td
    s[it]=sin(td)
    c[it]=cos(td)
}
trigPlot.addFunction(new plotfunction(dt,s))
trigPlot.addFunction(new plotfunction(t,c))
trigPlot.show()


// a12_2
trigPlot = new PlotFrame()
n=100; dt=2.0*PI/n
s=[];c=[]
(0..n).each{s[it]=sin(dt*it);c[it]=cos(dt*it)}
fp=new plotfunction(dt,s); fp.setMarker(true)
fp.setMarkerFill(true); fp.setName("sine")
trigPlot.addFunction(fp)
fp=new plotfunction(dt,c); fp.setMarker(true)
fp.setMarkerStyle(1);  fp.setName("cosine")
trigPlot.addFunction(fp)

trigPlot.setAutoColor(true)
trigPlot.Title("Trigonometric functions")
trigPlot.makeLegend()
trigPlot.xLabel("t")
trigPlot.yLabel("y")
trigPlot.vline(PI/2,java.awt.Color.green)
trigPlot.vline(PI,java.awt.Color.green)
trigPlot.vline(3*PI/2,java.awt.Color.green)
trigPlot.show()
