Skip to content

Commit 7bf10ac

Browse files
author
Manpreet
committed
added fields/oper
1 parent ca65af1 commit 7bf10ac

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package linalg.fields
2+
3+
import breeze.math.Complex
4+
5+
/**
6+
* Created by manpreet.singh on 27/01/16.
7+
*/
8+
object ComplexNumber {
9+
10+
def complexNumbers(): Unit = {
11+
val i = Complex.i
12+
// add
13+
println((1 + 2 * i) + (2 + 3 * i))
14+
15+
// sub
16+
println((1 + 2 * i) - (2 + 3 * i))
17+
18+
// divide
19+
println((5 + 10 * i) / (3 - 4 * i))
20+
21+
// mul
22+
println((1 + 2 * i) * (-3 + 6 * i))
23+
println((1 + 5 * i) * (-3 + 2 * i))
24+
25+
// neg
26+
println(-(1 + 2 * i))
27+
28+
// sum of complex numbers
29+
val x = List((5 + 7 * i), (1 + 3 * i), (13 + 17 * i))
30+
println(x.sum)
31+
32+
// product of complex numbers
33+
val x1 = List((5 + 7 * i), (1 + 3 * i), (13 + 17 * i))
34+
println(x1.product)
35+
36+
// sort list of complex numbers
37+
val x2 = List((5 + 7 * i), (1 + 3 * i), (13 + 17 * i))
38+
println(x2.sorted)
39+
40+
}
41+
42+
def main(args: Array[String]) {
43+
complexNumbers()
44+
}
45+
46+
47+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package linalg.fields
2+
3+
/**
4+
* Created by manpreet.singh on 27/01/16.
5+
*/
6+
trait GF2 {
7+
def + (that: GF2): GF2
8+
def * (that: GF2): GF2
9+
10+
def / (that: GF2) = that match {
11+
case Zero => throw new IllegalArgumentException("Div by 0")
12+
case _ => this
13+
}
14+
}
15+
16+
object Zero extends GF2 {
17+
override def toString = "Zero"
18+
def + (that: GF2) = that
19+
def * (that: GF2) = this
20+
}
21+
22+
object One extends GF2 {
23+
override def toString = "One"
24+
def + (that: GF2) = that match { case One => Zero ; case _ => this }
25+
def * (that: GF2) = that match { case One => this ; case _ => that }
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package linalg.fields
2+
3+
/**
4+
* Created by manpreet.singh on 27/01/16.
5+
*/
6+
object GaloisField {
7+
8+
// GF2 operations
9+
def GaloisField2(): Unit = {
10+
println(Zero + Zero)
11+
println(One + Zero)
12+
println(One + One)
13+
}
14+
15+
def main(args: Array[String]) {
16+
GaloisField2()
17+
}
18+
19+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package linalg.operations
2+
3+
import breeze.linalg._
4+
import breeze.numerics.{floor, ceil}
5+
6+
/**
7+
* Created by manpreet.singh on 28/01/16.
8+
*/
9+
object common {
10+
11+
// vector's
12+
val v1 = DenseVector(3, 7, 8.1, 4, 5)
13+
val v2 = DenseVector(1, 9, 3, 2.3, 8)
14+
15+
// column matrix (3 * 3)
16+
val m1 = DenseMatrix((2, 3, 1), (4, 5, 1), (6, 7, 1))
17+
val m2 = DenseMatrix((3, 4, 1), (2, 6, 1), (8, 2, 1))
18+
19+
// elementwise add operation
20+
def add(): Unit = {
21+
println(v1 + v2)
22+
println(m1 + m2)
23+
}
24+
25+
// elementwise mul operation
26+
def mul(): Unit = {
27+
println(v1 :* v2)
28+
println(m1 :* m2)
29+
}
30+
31+
// elementwise compare operation
32+
def compare(): Unit = {
33+
println(v1 :< v2)
34+
println(m1 :< m2)
35+
}
36+
37+
// inplace addition operation
38+
// def inplace(): Unit = {
39+
// println(v1 += 2)
40+
// println(m1 += 3)
41+
// }
42+
43+
// vector dot product operation
44+
def dot(): Unit = {
45+
println(v1 dot v2)
46+
}
47+
48+
// elementwise sum
49+
def sumof(): Unit = {
50+
println(sum(v1))
51+
}
52+
53+
// elementwise max
54+
def maxElement(): Unit = {
55+
println(max(v1))
56+
println(max(m1))
57+
}
58+
59+
// elementwise argmax
60+
def argmaxElement(): Unit = {
61+
println(argmax(v1))
62+
println(argmax(m1))
63+
}
64+
65+
// ceiling (Rounds numbers to the next highest integer)
66+
def ceiling(): Unit = {
67+
println(ceil(v1))
68+
println(ceil(m1))
69+
}
70+
71+
// floor (Rounds numbers to the next lowest integer.)
72+
def flooring(): Unit = {
73+
println(floor(v1))
74+
println(floor(m1))
75+
}
76+
77+
def main(args: Array[String]) {
78+
add()
79+
mul()
80+
compare()
81+
dot()
82+
sumof()
83+
maxElement()
84+
argmaxElement()
85+
ceiling()
86+
flooring()
87+
}
88+
}

0 commit comments

Comments
 (0)