eaebec39创建于 2024年10月24日历史提交
package scientific.matplot

import std.math.*
import std.unittest.*
import std.unittest.testmacro.*

import scientific.numbers.*
import scientific.linear.*
import scientific.stats.random.*
import scientific.stats.normal.*

/* Type for theta: Float64 */
foreign func c_polarhistogram(theta: CPointer<Unit>, theta_len: Int64, nbins: Int64):CPointer<Unit>

public func polarhistogram(theta: Vector<Float64>, nbins: Int64) {
    let size = theta.size()
    let handle = unsafe { c_polarhistogram(theta.ptr, size, nbins) }
    return Histogram(handle)
}

public func testPolarHist1() {
    let theta = vector([
        0.1, 1.1, 5.4, 3.4, 2.3, 4.5, 3.2,
        3.4, 5.6, 2.3, 2.1, 3.5, 0.6, 6.1
    ])
    polarhistogram(theta, 6)

    save("./tests/imgs/polar_hist/polarHist_1.svg", "svg")
    clear()
}

// different from example, do not know why, miss theta from 90 t0 270
public func testPolarHist2() {
    var m0: Random = Random(0)
    var m1: Random = Random(1)
    let r1 = rand(m0, 100000, 0.0, 1.0)
    let r2 = rand(m1, 100000, 0.0, 1.0)

    let theta = apply2(r1, r2, {x:Float64, y:Float64 => atan((x-0.5)/(2.0*(y-0.5)))})
    polarhistogram(theta, 25)
    save("./tests/imgs/polar_hist/polarHist_2.svg", "svg")
    clear()
}

public func testPolarHist3() {
    var m0: Random = Random(0)
    var m1: Random = Random(1)
    let r1 = rand(m0, 100000, 0.0, 1.0)
    let r2 = rand(m1, 100000, 0.0, 1.0)

    let theta = apply2(r1, r2, {x:Float64, y:Float64 => atan((x-0.5)/(2.0*(y-0.5)))})
    let h = polarhistogram(theta, 25)
    h.face_color("red")
    h.face_alpha(0.7)
    save("./tests/imgs/polar_hist/polarHist_3.svg", "svg")
    clear()
}

public func testPolarHist4() {
    var m0: Random = Random(0)
    var m1: Random = Random(1)
    let r1 = rand(m0, 100000, 0.0, 1.0)
    let r2 = rand(m1, 100000, 0.0, 1.0)

    let theta = apply2(r1, r2, {x:Float64, y:Float64 => atan((x-0.5)/(2.0*(y-0.5)))})
    let h = polarhistogram(theta, 25)
    h.stairs_only(true)
    save("./tests/imgs/polar_hist/polarHist_4.svg", "svg")
    clear()
}

// TODO: need randp
public func testPolarHist5(){

}

public func testPolarHist() {
    testPolarHist1()
    testPolarHist2()
    testPolarHist3()
    testPolarHist4()
}