Dev #194

Merged
altavir merged 266 commits from dev into master 2021-01-20 17:32:32 +03:00
4 changed files with 17 additions and 17 deletions
Showing only changes of commit 8b171ac3a3 - Show all commits

View File

@ -27,7 +27,7 @@
- `power(T, Int)` extension function has preconditions and supports `Field<T>` - `power(T, Int)` extension function has preconditions and supports `Field<T>`
- Memory objects have more preconditions (overflow checking) - Memory objects have more preconditions (overflow checking)
- `tg` function is renamed to `tan` (https://github.com/mipt-npm/kmath/pull/114) - `tg` function is renamed to `tan` (https://github.com/mipt-npm/kmath/pull/114)
- Gradle version: 6.3 -> 6.6 - Gradle version: 6.3 -> 6.6.1
- Moved probability distributions to commons-rng and to `kmath-prob` - Moved probability distributions to commons-rng and to `kmath-prob`
### Fixed ### Fixed

View File

@ -255,9 +255,9 @@ public class BigInt internal constructor(
} }
private fun addMagnitudes(mag1: Magnitude, mag2: Magnitude): Magnitude { private fun addMagnitudes(mag1: Magnitude, mag2: Magnitude): Magnitude {
val resultLength: Int = max(mag1.size, mag2.size) + 1 val resultLength = max(mag1.size, mag2.size) + 1
val result = Magnitude(resultLength) val result = Magnitude(resultLength)
var carry: TBase = 0UL var carry = 0uL
for (i in 0 until resultLength - 1) { for (i in 0 until resultLength - 1) {
val res = when { val res = when {
@ -265,20 +265,22 @@ public class BigInt internal constructor(
i >= mag2.size -> mag1[i].toULong() + carry i >= mag2.size -> mag1[i].toULong() + carry
else -> mag1[i].toULong() + mag2[i].toULong() + carry else -> mag1[i].toULong() + mag2[i].toULong() + carry
} }
result[i] = (res and BASE).toUInt() result[i] = (res and BASE).toUInt()
carry = (res shr BASE_SIZE) carry = (res shr BASE_SIZE)
} }
result[resultLength - 1] = carry.toUInt() result[resultLength - 1] = carry.toUInt()
return stripLeadingZeros(result) return stripLeadingZeros(result)
} }
private fun subtractMagnitudes(mag1: Magnitude, mag2: Magnitude): Magnitude { private fun subtractMagnitudes(mag1: Magnitude, mag2: Magnitude): Magnitude {
val resultLength: Int = mag1.size val resultLength = mag1.size
val result = Magnitude(resultLength) val result = Magnitude(resultLength)
var carry = 0L var carry = 0L
for (i in 0 until resultLength) { for (i in 0 until resultLength) {
var res: Long = var res =
if (i < mag2.size) mag1[i].toLong() - mag2[i].toLong() - carry if (i < mag2.size) mag1[i].toLong() - mag2[i].toLong() - carry
else mag1[i].toLong() - carry else mag1[i].toLong() - carry
@ -292,9 +294,9 @@ public class BigInt internal constructor(
} }
private fun multiplyMagnitudeByUInt(mag: Magnitude, x: UInt): Magnitude { private fun multiplyMagnitudeByUInt(mag: Magnitude, x: UInt): Magnitude {
val resultLength: Int = mag.size + 1 val resultLength = mag.size + 1
val result = Magnitude(resultLength) val result = Magnitude(resultLength)
var carry: ULong = 0UL var carry = 0uL
for (i in mag.indices) { for (i in mag.indices) {
val cur: ULong = carry + mag[i].toULong() * x.toULong() val cur: ULong = carry + mag[i].toULong() * x.toULong()
@ -307,11 +309,11 @@ public class BigInt internal constructor(
} }
private fun multiplyMagnitudes(mag1: Magnitude, mag2: Magnitude): Magnitude { private fun multiplyMagnitudes(mag1: Magnitude, mag2: Magnitude): Magnitude {
val resultLength: Int = mag1.size + mag2.size val resultLength = mag1.size + mag2.size
val result = Magnitude(resultLength) val result = Magnitude(resultLength)
for (i in mag1.indices) { for (i in mag1.indices) {
var carry: ULong = 0UL var carry = 0uL
for (j in mag2.indices) { for (j in mag2.indices) {
val cur: ULong = result[i + j].toULong() + mag1[i].toULong() * mag2[j].toULong() + carry val cur: ULong = result[i + j].toULong() + mag1[i].toULong() * mag2[j].toULong() + carry
@ -338,9 +340,7 @@ public class BigInt internal constructor(
return stripLeadingZeros(result) return stripLeadingZeros(result)
} }
} }
} }
private fun stripLeadingZeros(mag: Magnitude): Magnitude { private fun stripLeadingZeros(mag: Magnitude): Magnitude {
@ -366,7 +366,8 @@ public fun Int.toBigInt(): BigInt = BigInt(sign.toByte(), uintArrayOf(kotlin.mat
* Convert this [Long] to [BigInt] * Convert this [Long] to [BigInt]
*/ */
public fun Long.toBigInt(): BigInt = BigInt( public fun Long.toBigInt(): BigInt = BigInt(
sign.toByte(), stripLeadingZeros( sign.toByte(),
stripLeadingZeros(
uintArrayOf( uintArrayOf(
(kotlin.math.abs(this).toULong() and BASE).toUInt(), (kotlin.math.abs(this).toULong() and BASE).toUInt(),
((kotlin.math.abs(this).toULong() shr BASE_SIZE) and BASE).toUInt() ((kotlin.math.abs(this).toULong() shr BASE_SIZE) and BASE).toUInt()
@ -384,7 +385,6 @@ public fun UInt.toBigInt(): BigInt = BigInt(1, uintArrayOf(this))
*/ */
public fun ULong.toBigInt(): BigInt = BigInt( public fun ULong.toBigInt(): BigInt = BigInt(
1, 1,
stripLeadingZeros( stripLeadingZeros(
uintArrayOf( uintArrayOf(
(this and BASE).toUInt(), (this and BASE).toUInt(),

View File

@ -13,9 +13,9 @@ import kotlin.math.pow
* Polynomial coefficients without fixation on specific context they are applied to * Polynomial coefficients without fixation on specific context they are applied to
* @param coefficients constant is the leftmost coefficient * @param coefficients constant is the leftmost coefficient
*/ */
public /*inline*/ class Polynomial<T : Any>(public val coefficients: List<T>) { public inline class Polynomial<T : Any>(public val coefficients: List<T>)
public constructor(vararg coefficients: T) : this(coefficients.toList())
} public fun <T : Any> Polynomial(vararg coefficients: T): Polynomial<T> = Polynomial(coefficients.toList())
public fun Polynomial<Double>.value(): Double = coefficients.reduceIndexed { index, acc, d -> acc + d.pow(index) } public fun Polynomial<Double>.value(): Double = coefficients.reduceIndexed { index, acc, d -> acc + d.pow(index) }

View File

@ -10,7 +10,7 @@ public inline class ViktorBuffer(public val flatArray: F64FlatArray) : MutableBu
public override inline fun get(index: Int): Double = flatArray[index] public override inline fun get(index: Int): Double = flatArray[index]
override inline fun set(index: Int, value: Double) { public override inline fun set(index: Int, value: Double) {
flatArray[index] = value flatArray[index] = value
} }