pre-0.0.3 #46

Merged
altavir merged 75 commits from dev into master 2019-02-20 13:05:39 +03:00
6 changed files with 30 additions and 36 deletions
Showing only changes of commit 9da1a8c3e3 - Show all commits

View File

@ -17,9 +17,8 @@ interface ExpressionContext<T> {
} }
internal class VariableExpression<T>(val name: String, val default: T? = null) : Expression<T> { internal class VariableExpression<T>(val name: String, val default: T? = null) : Expression<T> {
override fun invoke(arguments: Map<String, T>): T { override fun invoke(arguments: Map<String, T>): T =
return arguments[name] ?: default ?: error("The parameter not found: $name") arguments[name] ?: default ?: error("Parameter not found: $name")
}
} }
internal class ConstantExpression<T>(val value: T) : Expression<T> { internal class ConstantExpression<T>(val value: T) : Expression<T> {

View File

@ -52,9 +52,8 @@ class PhantomHistogram<T : Comparable<T>>(
override val dimension: Int override val dimension: Int
get() = data.dimension get() = data.dimension
override fun iterator(): Iterator<PhantomBin<T>> { override fun iterator(): Iterator<PhantomBin<T>> =
return bins.asSequence().map { entry -> PhantomBin(entry.key, data[entry.value]) }.iterator() bins.asSequence().map { entry -> PhantomBin(entry.key, data[entry.value]) }.iterator()
}
override fun get(point: Point<out T>): PhantomBin<T>? { override fun get(point: Point<out T>): PhantomBin<T>? {
val template = bins.keys.find { it.contains(point) } val template = bins.keys.find { it.contains(point) }

View File

@ -90,7 +90,7 @@ abstract class LUDecomposition<T : Comparable<T>, F : Field<T>>(val matrix: Matr
} }
/** /**
* In-place transformation for [MutableNDArray], using given transformation for each element * In-place transformation for [MutableNDStructure], using given transformation for each element
*/ */
operator fun <T> MutableNDStructure<T>.set(i: Int, j: Int, value: T) { operator fun <T> MutableNDStructure<T>.set(i: Int, j: Int, value: T) {
this[intArrayOf(i, j)] = value this[intArrayOf(i, j)] = value
@ -181,9 +181,7 @@ abstract class LUDecomposition<T : Comparable<T>, F : Field<T>>(val matrix: Matr
* @return the pivot permutation vector * @return the pivot permutation vector
* @see .getP * @see .getP
*/ */
fun getPivot(): IntArray { fun getPivot(): IntArray = pivot.copyOf()
return pivot.copyOf()
}
} }

View File

@ -29,8 +29,8 @@ fun List<Double>.toVector() = Vector.real(this.size) { this[it] }
/** /**
* Convert matrix to vector if it is possible * Convert matrix to vector if it is possible
*/ */
fun <T : Any, F : Ring<T>> Matrix<T, F>.toVector(): Vector<T, F> { fun <T : Any, F : Ring<T>> Matrix<T, F>.toVector(): Vector<T, F> =
return if (this.numCols == 1) { if (this.numCols == 1) {
// if (this is ArrayMatrix) { // if (this is ArrayMatrix) {
// //Reuse existing underlying array // //Reuse existing underlying array
// ArrayVector(ArrayVectorSpace(rows, context.field, context.ndFactory), array) // ArrayVector(ArrayVectorSpace(rows, context.field, context.ndFactory), array)
@ -38,9 +38,8 @@ fun <T : Any, F : Ring<T>> Matrix<T, F>.toVector(): Vector<T, F> {
// //Generic vector // //Generic vector
// vector(rows, context.field) { get(it, 0) } // vector(rows, context.field) { get(it, 0) }
// } // }
Vector.generic(numRows, context.ring) { get(it, 0) } Vector.generic(numRows, context.ring) { get(it, 0) }
} else error("Can't convert matrix with more than one column to vector") } else error("Can't convert matrix with more than one column to vector")
}
fun <T : Any, R : Ring<T>> Vector<T, R>.toMatrix(): Matrix<T, R> { fun <T : Any, R : Ring<T>> Vector<T, R>.toMatrix(): Matrix<T, R> {
// val context = StructureMatrixContext(size, 1, context.space) // val context = StructureMatrixContext(size, 1, context.space)
@ -57,9 +56,8 @@ fun <T : Any, R : Ring<T>> Vector<T, R>.toMatrix(): Matrix<T, R> {
} }
object VectorL2Norm : Norm<Vector<out Number, *>, Double> { object VectorL2Norm : Norm<Vector<out Number, *>, Double> {
override fun norm(arg: Vector<out Number, *>): Double { override fun norm(arg: Vector<out Number, *>): Double =
return kotlin.math.sqrt(arg.asSequence().sumByDouble { it.toDouble() }) kotlin.math.sqrt(arg.asSequence().sumByDouble { it.toDouble() })
}
} }
typealias RealVector = Vector<Double, RealField> typealias RealVector = Vector<Double, RealField>

View File

@ -8,30 +8,29 @@ package scientifik.kmath.misc
* *
* If step is negative, the same goes from upper boundary downwards * If step is negative, the same goes from upper boundary downwards
*/ */
fun ClosedFloatingPointRange<Double>.toSequence(step: Double): Sequence<Double> { fun ClosedFloatingPointRange<Double>.toSequence(step: Double): Sequence<Double> =
return when { when {
step == 0.0 -> error("Zero step in double progression") step == 0.0 -> error("Zero step in double progression")
step > 0 -> sequence { step > 0 -> sequence {
var current = start var current = start
while (current <= endInclusive) { while (current <= endInclusive) {
yield(current) yield(current)
current += step current += step
}
}
else -> sequence {
var current = endInclusive
while (current >= start) {
yield(current)
current += step
}
} }
} }
else -> sequence {
var current = endInclusive
while (current >= start) {
yield(current)
current += step
}
}
}
}
/** /**
* Convert double range to array of evenly spaced doubles, where the size of array equals [numPoints] * Convert double range to array of evenly spaced doubles, where the size of array equals [numPoints]
*/ */
fun ClosedFloatingPointRange<Double>.toGrid(numPoints: Int): DoubleArray { fun ClosedFloatingPointRange<Double>.toGrid(numPoints: Int): DoubleArray {
if (numPoints < 2) error("Can't buffered grid with less than two points") if (numPoints < 2) error("Can't create generic grid with less than two points")
return DoubleArray(numPoints) { i -> start + (endInclusive - start) / (numPoints - 1) * i } return DoubleArray(numPoints) { i -> start + (endInclusive - start) / (numPoints - 1) * i }
} }

View File

@ -34,6 +34,7 @@ interface Space<T> {
operator fun T.div(k: Number) = multiply(this, 1.0 / k.toDouble()) operator fun T.div(k: Number) = multiply(this, 1.0 / k.toDouble())
operator fun Number.times(b: T) = b * this operator fun Number.times(b: T) = b * this
fun Iterable<T>.sum(): T = fold(zero) { left, right -> left + right } fun Iterable<T>.sum(): T = fold(zero) { left, right -> left + right }
fun Sequence<T>.sum(): T = fold(zero) { left, right -> left + right } fun Sequence<T>.sum(): T = fold(zero) { left, right -> left + right }
} }