Skip to content

Commit 5f0aa5c

Browse files
committed
scala 3 code formatting
1 parent dae5c99 commit 5f0aa5c

File tree

7 files changed

+143
-136
lines changed

7 files changed

+143
-136
lines changed

.scalafmt.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
version = 3.8.3
2-
runner.dialect = scala213
2+
runner.dialect = scala3

build.sbt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ libraryDependencies ++= Seq(
2121
("org.ddahl" %% "rscala" % "3.2.19").cross(CrossVersion.for3Use2_13)
2222
)
2323

24-
2524
mdocIn := file("mdoc/")
2625

2726
mdocOut := file("docs/")

examples/src/main/scala/FBm.scala

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,47 @@ Fractional Brownian motion via and inverse Discrete Cosine Transform (DCT)
77

88
import scalaglm.Utils.*
99

10-
import breeze.linalg.*
11-
import breeze.numerics.*
12-
import breeze.stats.distributions.*
13-
import breeze.stats.distributions.Rand.VariableSeed.randBasis
14-
import java.awt.image.BufferedImage
15-
16-
def dm2bi(m: DenseMatrix[Double]): BufferedImage =
17-
val canvas = new BufferedImage(m.cols, m.rows, BufferedImage.TYPE_INT_RGB)
18-
val wr = canvas.getRaster
19-
val mx = max(m)
20-
val mn = min(m)
21-
(0 until m.cols).foreach { x =>
22-
(0 until m.rows).foreach { y =>
23-
val shade = round(255 * (m(y, x) - mn) / (mx - mn)).toInt
24-
wr.setSample(x, y, 0, shade) // R
25-
wr.setSample(x, y, 1, shade) // G
26-
wr.setSample(x, y, 2, 255) // B
27-
}
10+
import breeze.linalg.*
11+
import breeze.numerics.*
12+
import breeze.stats.distributions.*
13+
import breeze.stats.distributions.Rand.VariableSeed.randBasis
14+
import java.awt.image.BufferedImage
15+
16+
def dm2bi(m: DenseMatrix[Double]): BufferedImage =
17+
val canvas = new BufferedImage(m.cols, m.rows, BufferedImage.TYPE_INT_RGB)
18+
val wr = canvas.getRaster
19+
val mx = max(m)
20+
val mn = min(m)
21+
(0 until m.cols).foreach { x =>
22+
(0 until m.rows).foreach { y =>
23+
val shade = round(255 * (m(y, x) - mn) / (mx - mn)).toInt
24+
wr.setSample(x, y, 0, shade) // R
25+
wr.setSample(x, y, 1, shade) // G
26+
wr.setSample(x, y, 2, 255) // B
2827
}
29-
canvas
30-
31-
def showImage(m: DenseMatrix[Double]): Unit =
32-
import breeze.plot.*
33-
val fig = Figure("fBm")
34-
fig.width = 1200
35-
fig.height = 1200
36-
val p0 = fig.subplot(0)
37-
p0 += image(m)
38-
fig.refresh()
39-
40-
@main def fBmSim() =
41-
val N = 1024
42-
val H = 0.9
43-
val sd = DenseMatrix.tabulate(N, N){(j, k) =>
44-
if (j*j + k*k < 9) 0.0 else
45-
math.pow(j*j + k*k, -(H + 1)/2) }
46-
val M = sd map (s => Gaussian(0.0, s).draw())
47-
val m = dct2(M, true)
48-
javax.imageio.ImageIO.write(dm2bi(m), "png", new java.io.File("fBm.png"))
49-
showImage(m)
50-
51-
28+
}
29+
canvas
30+
31+
def showImage(m: DenseMatrix[Double]): Unit =
32+
import breeze.plot.*
33+
val fig = Figure("fBm")
34+
fig.width = 1200
35+
fig.height = 1200
36+
val p0 = fig.subplot(0)
37+
p0 += image(m)
38+
fig.refresh()
39+
40+
@main def fBmSim() =
41+
val N = 1024
42+
val H = 0.9
43+
val sd = DenseMatrix.tabulate(N, N) { (j, k) =>
44+
if (j * j + k * k < 9) 0.0
45+
else
46+
math.pow(j * j + k * k, -(H + 1) / 2)
47+
}
48+
val M = sd map (s => Gaussian(0.0, s).draw())
49+
val m = dct2(M, true)
50+
javax.imageio.ImageIO.write(dm2bi(m), "png", new java.io.File("fBm.png"))
51+
showImage(m)
5252

5353
// eof
54-

examples/src/main/scala/IrisPca.scala

Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,64 +9,76 @@ import scalaglm.*
99

1010
@main def irisPca() =
1111

12-
val url = "http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
13-
val fileName = "iris.csv"
14-
val imap = Map(
15-
"Iris-setosa" -> 0,
16-
"Iris-versicolor" -> 1,
17-
"Iris-virginica" -> 2)
12+
val url =
13+
"http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
14+
val fileName = "iris.csv"
15+
val imap =
16+
Map("Iris-setosa" -> 0, "Iris-versicolor" -> 1, "Iris-virginica" -> 2)
1817

19-
// download the file to disk if it hasn't been already
20-
val file = new java.io.File(fileName)
21-
if !file.exists then
22-
val s = new java.io.PrintWriter(file)
23-
val data = scala.io.Source.fromURL(url).getLines()
24-
data.foreach(l => s.write(l.trim.split(',').
25-
map(x => imap.getOrElse(x, x)).mkString("", ",", "\n")))
26-
s.close
18+
// download the file to disk if it hasn't been already
19+
val file = new java.io.File(fileName)
20+
if !file.exists then
21+
val s = new java.io.PrintWriter(file)
22+
val data = scala.io.Source.fromURL(url).getLines()
23+
data.foreach(l =>
24+
s.write(
25+
l.trim.split(',').map(x => imap.getOrElse(x, x)).mkString("", ",", "\n")
26+
)
27+
)
28+
s.close
2729

28-
// read the file from disk
29-
val mat = csvread(file)
30-
println("Mat Dim: " + mat.rows + " " + mat.cols)
31-
val x = mat(::, 0 to 3)
32-
println("X Dim: " + x.rows + " " + x.cols)
33-
val clas = mat(::, 4).toDenseVector
30+
// read the file from disk
31+
val mat = csvread(file)
32+
println("Mat Dim: " + mat.rows + " " + mat.cols)
33+
val x = mat(::, 0 to 3)
34+
println("X Dim: " + x.rows + " " + x.cols)
35+
val clas = mat(::, 4).toDenseVector
3436

35-
println("PCA with built-in Breeze version (like R princomp):")
36-
val pca = new PCA(x, covmat(x))
37-
println("Loadings:")
38-
println(pca.loadings)
39-
println("Stdev:")
40-
println(pca.sdev)
41-
println(pca.scores(0 to 5, ::))
37+
println("PCA with built-in Breeze version (like R princomp):")
38+
val pca = new PCA(x, covmat(x))
39+
println("Loadings:")
40+
println(pca.loadings)
41+
println("Stdev:")
42+
println(pca.sdev)
43+
println(pca.scores(0 to 5, ::))
4244

43-
println("Now my version (like R prcomp):")
44-
val myPca = Pca(x, List("S-L","S-W","P-L","P-W"))
45-
println(myPca.loadings) // loadings transposed
46-
println(myPca.sdev)
47-
myPca.summary
48-
println("Scores:")
49-
println(myPca.scores(0 to 5, ::))
50-
myPca.plots.saveas("IrisPcaDiag.png")
45+
println("Now my version (like R prcomp):")
46+
val myPca = Pca(x, List("S-L", "S-W", "P-L", "P-W"))
47+
println(myPca.loadings) // loadings transposed
48+
println(myPca.sdev)
49+
myPca.summary
50+
println("Scores:")
51+
println(myPca.scores(0 to 5, ::))
52+
myPca.plots.saveas("IrisPcaDiag.png")
5153

52-
// scatter plot first 2 principal components
53-
import breeze.plot.*
54-
val fig = Figure("PCA")
55-
val p = fig.subplot(0)
56-
val ind0 = (0 until x.rows) filter (i => clas(i) == 0)
57-
p += plot(myPca.scores(ind0, 0).toDenseVector,
58-
myPca.scores(ind0, 1).toDenseVector, '.', colorcode = "blue")
59-
val ind1 = (0 until x.rows) filter (i => clas(i) == 1)
60-
p += plot(myPca.scores(ind1, 0).toDenseVector,
61-
myPca.scores(ind1, 1).toDenseVector, '.', colorcode = "red")
62-
val ind2 = (0 until x.rows) filter (i => clas(i) == 2)
63-
p += plot(myPca.scores(ind2, 0).toDenseVector,
64-
myPca.scores(ind2, 1).toDenseVector, '.', colorcode = "green")
65-
fig.saveas("IrisPca.png")
66-
67-
// Test without variable names
68-
Pca(x).summary
54+
// scatter plot first 2 principal components
55+
import breeze.plot.*
56+
val fig = Figure("PCA")
57+
val p = fig.subplot(0)
58+
val ind0 = (0 until x.rows) filter (i => clas(i) == 0)
59+
p += plot(
60+
myPca.scores(ind0, 0).toDenseVector,
61+
myPca.scores(ind0, 1).toDenseVector,
62+
'.',
63+
colorcode = "blue"
64+
)
65+
val ind1 = (0 until x.rows) filter (i => clas(i) == 1)
66+
p += plot(
67+
myPca.scores(ind1, 0).toDenseVector,
68+
myPca.scores(ind1, 1).toDenseVector,
69+
'.',
70+
colorcode = "red"
71+
)
72+
val ind2 = (0 until x.rows) filter (i => clas(i) == 2)
73+
p += plot(
74+
myPca.scores(ind2, 0).toDenseVector,
75+
myPca.scores(ind2, 1).toDenseVector,
76+
'.',
77+
colorcode = "green"
78+
)
79+
fig.saveas("IrisPca.png")
6980

81+
// Test without variable names
82+
Pca(x).summary
7083

7184
// eof
72-

examples/src/main/scala/LinearRegression.scala

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,33 @@ import breeze.linalg.*
1010

1111
@main def linearRegression() =
1212

13-
val url = "http://archive.ics.uci.edu/ml/machine-learning-databases/00291/airfoil_self_noise.dat"
14-
val fileName = "self-noise.csv"
15-
16-
// download the file to disk if it hasn't been already
17-
val file = new java.io.File(fileName)
18-
if !file.exists then
19-
val s = new java.io.PrintWriter(file)
20-
val data = scala.io.Source.fromURL(url).getLines()
21-
data.foreach(l => s.write(l.trim.split('\t').filter(_ != "").mkString("", ",", "\n")))
22-
s.close
23-
24-
// read the file from disk
25-
val mat = csvread(file)
26-
println("Dim: " + mat.rows + " " + mat.cols)
27-
val fig = Utils.pairs(mat, List("Freq", "Angle", "Chord", "Velo", "Thick", "Sound"))
28-
val y = mat(::, 5) // response is the final column
29-
val X = mat(::, 0 to 4)
30-
val mod = Lm(y, X, List("Freq", "Angle", "Chord", "Velo", "Thick"))
31-
mod.summary
32-
mod.plots.saveas("LinearRegression.png")
33-
34-
// test without name list
35-
Lm(y,X,false).summary
36-
Lm(y,X).summary
37-
13+
val url =
14+
"http://archive.ics.uci.edu/ml/machine-learning-databases/00291/airfoil_self_noise.dat"
15+
val fileName = "self-noise.csv"
16+
17+
// download the file to disk if it hasn't been already
18+
val file = new java.io.File(fileName)
19+
if !file.exists then
20+
val s = new java.io.PrintWriter(file)
21+
val data = scala.io.Source.fromURL(url).getLines()
22+
data.foreach(l =>
23+
s.write(l.trim.split('\t').filter(_ != "").mkString("", ",", "\n"))
24+
)
25+
s.close
26+
27+
// read the file from disk
28+
val mat = csvread(file)
29+
println("Dim: " + mat.rows + " " + mat.cols)
30+
val fig =
31+
Utils.pairs(mat, List("Freq", "Angle", "Chord", "Velo", "Thick", "Sound"))
32+
val y = mat(::, 5) // response is the final column
33+
val X = mat(::, 0 to 4)
34+
val mod = Lm(y, X, List("Freq", "Angle", "Chord", "Velo", "Thick"))
35+
mod.summary
36+
mod.plots.saveas("LinearRegression.png")
37+
38+
// test without name list
39+
Lm(y, X, false).summary
40+
Lm(y, X).summary
3841

3942
// eof
40-

examples/src/main/scala/LogisticRegression.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ import breeze.linalg.*
1010

1111
@main def logisticRegression() =
1212

13-
val url = "https://archive.ics.uci.edu/ml/machine-learning-databases/00267/data_banknote_authentication.txt"
13+
val url =
14+
"https://archive.ics.uci.edu/ml/machine-learning-databases/00267/data_banknote_authentication.txt"
1415
val fileName = "banknote.csv"
1516

1617
// download the file to disk if it hasn't been already
1718
val file = new java.io.File(fileName)
1819
if !file.exists then
1920
val s = new java.io.PrintWriter(file)
2021
val data = scala.io.Source.fromURL(url).getLines()
21-
data.foreach(l => s.write(l+"\n"))
22+
data.foreach(l => s.write(l + "\n"))
2223
s.close
2324

2425
// read the file from disk
@@ -27,10 +28,8 @@ import breeze.linalg.*
2728
val y = mat(::, 4) // response is the final column
2829
val X = mat(::, 0 to 3)
2930
// println(y)
30-
val mod = Glm(y, X, List("Var","Skew","Kurt","Entropy"), LogisticGlm)
31+
val mod = Glm(y, X, List("Var", "Skew", "Kurt", "Entropy"), LogisticGlm)
3132
// println(mod.coefficients)
3233
mod.summary
3334

34-
3535
// eof
36-

examples/src/main/scala/PolyFit.scala

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,33 @@ PolyFit.scala
33
44
*/
55

6-
76
import breeze.linalg.*
87
import breeze.numerics.*
98
import breeze.stats.distributions.Gaussian
109
import breeze.stats.distributions.Rand.VariableSeed.randBasis
1110
import scalaglm.*
1211

13-
1412
@main def polyFit() =
1513

1614
// simulate some synthetic data
1715
val n = 500
1816
val x = linspace(2.0, 5.0, n)
19-
val yt = 0.5*x + sin(x*x)
17+
val yt = 0.5 * x + sin(x * x)
2018
val y = yt + DenseVector(Gaussian(0.0, 1.0).sample(n).toArray)
2119

2220
// scatter plot
2321
import breeze.plot.*
2422
val fig = Figure("Synthetic data")
2523
val p = fig.subplot(0)
26-
p += plot(x, y, '+', name="Data")
27-
p += plot(x, yt, name="Truth")
24+
p += plot(x, y, '+', name = "Data")
25+
p += plot(x, yt, name = "Truth")
2826

2927
// some polynomial fits
3028
(1 to 17 by 4).foreach(i =>
3129
val lm = Lm(y, Basis.poly(x, i))
32-
p += plot(x, lm.fitted, name="P"+i)
30+
p += plot(x, lm.fitted, name = "P" + i)
3331
)
3432
p.legend = true
3533
p.title = "Polynomial fits"
3634

37-
3835
// eof
39-

0 commit comments

Comments
 (0)