summaryrefslogtreecommitdiff
path: root/test/files/run/Course-2002-04.scala
blob: 61deb96a11fabf1dbbacccca1cd92443eb4fb6e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//############################################################################
// Programmation IV - 2002 - Week 04
//############################################################################
// $Id$

object M0 {

  def quicksort[a] (less : (a,a) => Boolean) (xs : List[a]) : List[a] = {
    if (xs.isEmpty)
      xs
    else {
      val pivot : a = xs.head;
      val smaller : List[a] =
        quicksort(less)(xs.tail.filter(elem => less(elem, pivot)));
      val greaterOrEqual : List[a] =
        quicksort(less)(xs.tail.filter(elem => !less(elem, pivot)));
      smaller ::: List(pivot) ::: greaterOrEqual
    }
  }

  def test = {
    val isort = quicksort[Int]((x,y) => x < y);
    val list0 = List(6,3,1,8,7,1,2,5,8,4,3,4,8);
    val list1 = quicksort[Int]((x,y) => x < y)(list0);
    val list2 = quicksort[Int]((x,y) => x < y)(list1);
    val list3 = isort(list0);
    val list4 = isort(list1);
    val list5 = quicksort[Int]((x,y) => x >= y)(list0);
    val list6 = quicksort[Int]((x,y) => x >= y)(list1);

    System.out.println("list0 = " + list0);
    System.out.println("list1 = " + list1);
    System.out.println("list2 = " + list2);
    System.out.println("list3 = " + list3);
    System.out.println("list4 = " + list4);
    System.out.println("list5 = " + list5);
    System.out.println("list6 = " + list6);
    System.out.println();
  }
}

//############################################################################

object M1 {

  def horner (x : Double, coefs : List[Double]) : Double = {
    if (coefs.isEmpty)
      0
    else
      horner(x, coefs.tail) * x + coefs.head
  }

  def test = {
    val poly = List(9.0,5.0,7.0,5.0);
    System.out.println("f(x) = 5x^3+7x^2+5x+9");
    System.out.println("f(0) = " + horner(0, poly));
    System.out.println("f(1) = " + horner(1, poly));
    System.out.println("f(2) = " + horner(2, poly));
    System.out.println("f(3) = " + horner(3, poly));
    System.out.println();
  }
}

//############################################################################

object M2 {

  def dotproduct (v : List[Double], w : List[Double]) : Double = {
    if (v.isEmpty)
      0
    else
      (v.head * w.head) + dotproduct(v.tail, w.tail)
  }

  def matrixTimesVector (m : List[List[Double]], v : List[Double])
                        : List[Double] = {
    m.map(row => dotproduct(row, v))
  }

  def transpose(m : List[List[Double]]) : List[List[Double]] = {
    if (m.isEmpty || m.head.isEmpty)
      List()
    else
      m.map(row => row.head) :: transpose (m.map (row => row.tail))
  }

  def matrixTimesMatrix(m1 : List[List[Double]], m2 : List[List[Double]])
                       : List[List[Double]] = {
    val columns = transpose(m2);
    m1.map(row => matrixTimesVector(columns, row))
  }

  def test = {
    val v1 = List(2.0,3.0,4.0);
    val v2 = List(6.0,7.0,8.0);
    def id = List(List(1.0,0.0,0.0),List(0.0,1.0,0.0),List(0.0,0.0,1.0));
    def m1 = List(List(2.0,0.0,0.0),List(0.0,2.0,0.0),List(0.0,0.0,2.0));
    def m2 = List(List(1.0,2.0,3.0),List(4.0,5.0,6.0),List(7.0,8.0,9.0));

    def v = List(2.0,3.0,4.0);

    System.out.println("v1        = " + v1);
    System.out.println("v2        = " + v2);
    System.out.println();

    System.out.println("id        = " + id);
    System.out.println("m1        = " + m1);
    System.out.println("m2        = " + m2);
    System.out.println();

    System.out.println("v1 * v1   = " + dotproduct(v1,v1));
    System.out.println("v1 * v2   = " + dotproduct(v1,v2));
    System.out.println("v2 * v1   = " + dotproduct(v2,v1));
    System.out.println("v1 * v2   = " + dotproduct(v1,v2));
    System.out.println();

    System.out.println("id * v1   = " + matrixTimesVector(id,v1));
    System.out.println("m1 * v1   = " + matrixTimesVector(m1,v1));
    System.out.println("m2 * v1   = " + matrixTimesVector(m2,v1));
    System.out.println();

    System.out.println("trn(id)   = " + transpose(id));
    System.out.println("trn(m1)   = " + transpose(m1));
    System.out.println("trn(m2)   = " + transpose(m2));
    System.out.println();

    System.out.println("[v1] * id = " + matrixTimesMatrix(List(v1),id));
    System.out.println("[v1] * m1 = " + matrixTimesMatrix(List(v1),m1));
    System.out.println("[v1] * m2 = " + matrixTimesMatrix(List(v1),m2));
    System.out.println();

    System.out.println("id * [v1] = " + matrixTimesMatrix(id,List(v1)));
    System.out.println("m1 * [v1] = " + matrixTimesMatrix(m1,List(v1)));
    System.out.println("m2 * [v1] = " + matrixTimesMatrix(m2,List(v1)));
    System.out.println();

    System.out.println("id * id   = " + matrixTimesMatrix(id,id));
    System.out.println("id * m1   = " + matrixTimesMatrix(id,m1));
    System.out.println("m1 * id   = " + matrixTimesMatrix(m1,id));
    System.out.println("m1 * m1   = " + matrixTimesMatrix(m1,m1));
    System.out.println("id * m2   = " + matrixTimesMatrix(id,m2));
    System.out.println("m2 * id   = " + matrixTimesMatrix(m2,id));
    System.out.println("m1 * m2   = " + matrixTimesMatrix(m1,m2));
    System.out.println("m2 * m1   = " + matrixTimesMatrix(m2,m1));
    System.out.println("m2 * m2   = " + matrixTimesMatrix(m2,m2));
    System.out.println();
  }
}

//############################################################################

object Test {
  def main(args: Array[String]): Unit = {
    M0.test;
    M1.test;
    M2.test;
    ()
  }
}

//############################################################################