A new language is best learned through examples. In this section, we collected a number of examples of math scenarios using S2, so you have a better idea of what S2 is capable of. In the other sections of the course, you will learn S2 APIs and math concepts in depth. The S2 platform also allows us to develop data-driven solutions for scientific and business issues.

Using S2 on S2 IDE

If you are using S2 on S2 IDE, please make sure to write the magical keyword %use s2 on a cell and execute the cell before running any line of code. When you have this magic keyword, the S2 IDE will recognize that you are using the S2.

				
					// ALWAYS START THIS MAGICAL KEYWORD USING S2 or nothing will work
%use s2
				
			

Using S2 for Linear Algebra

				
					// define a vector
var v = DenseVector(arrayOf(1.1, -2.2, 3.3))
println(v)

// define a matrix
var A = DenseMatrix(arrayOf(
    doubleArrayOf(1.0, 2.0, 3.0),
    doubleArrayOf(4.0, 5.0, 6.0)))
println(A)

// B = Av
val B = A.multiply(v)
println(B)
				
			
OUTPUT: [1.100000, -2.200000, 3.300000] 
2x3
	[,1] [,2] [,3] 
[1,] 1.000000, 2.000000, 3.000000, 
[2,] 4.000000, 5.000000, 6.000000, 
[6.600000, 13.200000]

Using S2 for Calculus

				
					// define a function f(x)
val f: UnivariateRealFunction = object : AbstractUnivariateRealFunction() {
    override fun evaluate(x: Double): Double {
        return x.pow(2) + 2*x + 1 // x^2 + 2x + 1
    }
}

// find root for a function
val solver = NewtonRoot(1e-9, 10)
val root: Double = solver.solve(f, 0.5)
println(String.format("f(%f) = %f", root, f.evaluate(root)))
				
			
OUTPUT: f(-0.998535) = 0.000002
				
					// plot a function
val p = JGnuplot(false)
p.addPlot("x*x + 2*x + 1")
p.plot()
				
			
OUTPUT:
				
					// optimization
val solver = BrentMinimizer(1e-15, 20)
val soln = solver.solve(f)
val xmin: Double = soln.minimizer()
val fmin: Double = f.evaluate(xmin)
println(String.format("f(%f) = %f", xmin, fmin))
				
			
OUTPUT: f(0.000000) = 1.000000
				
					// differentiate f w.r.t. x once
val df1: UnivariateRealFunction = FiniteDifference(f, 1, FiniteDifference.Type.CENTRAL) // 2x + 2
df1.evaluate(1.0)

// integration
val integrator: Integrator = Riemann()
val I: Double = integrator.integrate(df1, 0.0, 1.0) // ∫_[0,1] (2x + 2) dx = x^2 + 2x
I
				
			
OUTPUT: 3.000000000218579

Using S2 for Statistics

				
					// statistics
val gaussian = NormalRNG(0.0, 1.0) // a standard Gaussian random number generator
val numbers: MutableList = mutableListOf()
for (i in 1..10000) { // generate random numbers
    val x: Double = gaussian.nextDouble()
    numbers.add(x)
}
val arr = numbers.toDoubleArray()
println(Mean(arr).value())
println(Variance(arr).standardDeviation())
println(Skewness(arr).value())
println(Kurtosis(arr).value())
				
			
OUTPUT: 0.007440710227587757
0.9934094869218749
-3.733142452297809E-4
-0.003197798050856182

Using S2 to Process Large-scale Data

				
					// Download stock data for "AAPL" between two dates.
val aapl = s2.dataFrame("stock", "AAPL", "2020-01-01", "2020-11-01")
aapl
				
			
OUTPUT: A DataFrame: 211 x 10
     ticker         date     open     high      low    close      volume   dividends   closeunadj
 1     AAPL   2020-01-02    74.06    75.15   73.797   75.088   135647456           0       300.35
 2     AAPL   2020-01-03   74.287   75.145   74.125   74.358   146535512           0       297.43
 3     AAPL   2020-01-06   73.448    74.99   73.188    74.95   117288824           0        299.8
 4     AAPL   2020-01-07    74.96   75.225    74.37   74.597   111510620           0       298.39
 5     AAPL   2020-01-08    74.29    76.11   74.289   75.797   132363784           0       303.19
 6     AAPL   2020-01-09   76.809   77.608    76.55   77.407   167082252           0       309.63
 7     AAPL   2020-01-10    77.65   78.168   77.062   77.582   140869088           0       310.33
 8     AAPL   2020-01-13    77.91   79.267   77.787    79.24   120114968           0       316.96
 9     AAPL   2020-01-14   79.175   79.392   78.043    78.17   162613828           0       312.68
10     AAPL   2020-01-15   77.963   78.875   77.388   77.835   121923528           0       311.34
and 201 more rows, and and 1 more variables: