Skip to content

math ​

原文: https://finproducts.atlassian.net/wiki/spaces/JIDD3/pages/34793587190/FIN+5.3.1+math 中文整理基于 FIN 5.3.1

导读: 数学工具和函数

以下为 API 参考,保留英文以便对照源码;目录已列出(英文标题便于检索);概述已尽量中译。


目录(导读) ​

  • mixins
  • classes
  • docs
  • Overview
  • Matrix
  • Slot Details
    • abs
    • and
    • bitLen
    • clearBit
    • compare
    • decrement
    • defVal
    • div
    • divInt
    • equals
    • flipBit
    • fromStr
    • hash
    • increment
    • makeBuf
    • makeInt
    • max
    • min
    • minus
    • minusInt
    • mod
    • modInt
    • mult
    • multInt
    • negate
    • not
    • one
    • or
    • plus
    • plusInt
    • pow
    • setBit
    • shiftl
    • shiftr
    • signum
    • testBit
    • toBuf
    • toDecimal
    • toFloat
    • toInt
    • toRadix
    • toStr
    • xor
    • zero
    • matrix
    • cofactor
    • determinant
    • fill
    • get
    • inverse
    • isSquare
    • multScalar
    • numCols
    • numRows
    • set
    • transpose

概述(中译) ​

math pod 定义了用于执行数学计算的标准类和实用工具。


API 正文(英文) ​

Matrix ​

The library includes a Matrix API and a default implementation. A matrix can be created using the Math.matrix utility.

matrix := Math.matrix(2,2)

BigInt ​

const class BigInt : Obj

Immutable arbitrary-precision integer.

Constructors

fromStrParse a Str into a BigInt using the specified radix.
makeBufTranslates a byte array containing the two's-complement binary representation of a BigInt into a BigInt.
makeIntReturns a BigInt whose value is equal to that of the specified Int.

Fields

defValDefault value is 0.
one
zero

Methods

absReturn the absolute value of this integer.
andBitwise-and of this and b.
bitLenReturns the number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit.
clearBitSet the given bit to 0.
compareCompare based on integer value.
decrementDecrement by one.
divDivide this by b.
divIntDivide this by b.
equalsReturn true if same both represent that same integer value.
flipBitFlip the given bit between 0 and 1.
hashThe hash for a BigInt is platform dependent.
incrementIncrement by one.
maxReturn the larger of this and the specified BigInt values.
minReturn the smaller of this and the specified BigInt values.
minusSubtract b from this.
minusIntSubtract b from this.
modReturn remainder of this divided by b.
modIntReturn remainder of this divided by b.
multMultiply this with b.
multIntMultiply this with b.
negateNegative of this.
notBitwise not/inverse of this.
orBitwise-or of this and b.
plusAdd this with b.
plusIntAdd this with b.
powReturn this value raised to the specified power.
setBitSet the given bit to 1.
shiftlBitwise left shift of this by b.
shiftrBitwise arithmetic right-shift of this by b.
signum-1, 0, 1 if the BigInt is negative, zero, or positive.
testBitReturn true if given bit is 1.
toBufReturns a byte array containing the two's-complement representation of this BigInt.
toDecimalConvert the number to an Decimal.
toFloatConvert the number to an Float.
toIntConvert the number to an Int.
toRadixReturn string representation in given radix.
toStrReturn decimal string representation.
xorBitwise-exclusive-or of this and b.

Slot Details ​


abs ​

BigInt abs()

Return the absolute value of this integer. If this value is positive then return this, otherwise return the negation.


and ​

BigInt and(Obj b)

Bitwise-and of this and b.


bitLen ​

Int bitLen()

Returns the number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit.


clearBit ​

BigInt clearBit(Int b)

Set the given bit to 0. Equivalent to this.and(1.shiftl(b).not).


compare ​

virtual override Int compare(Obj obj)

Compare based on integer value.


decrement ​

@Operator BigInt decrement()

Decrement by one. Shortcut is --a or a--.


defVal ​

const static BigInt defVal

Default value is 0.


div ​

@Operator BigInt div(BigInt b)

Divide this by b. Shortcut is a/b.


divInt ​

@Operator BigInt divInt(Int b)

Divide this by b. Shortcut is a/b.


equals ​

virtual override Bool equals(Obj? obj)

Return true if same both represent that same integer value.


flipBit ​

BigInt flipBit(Int b)

Flip the given bit between 0 and 1. Equivalent to this.xor(1.shiftl(b)).


fromStr ​

static new fromStr(Str s, Int radix := 10, Bool checked := true)

Parse a Str into a BigInt using the specified radix. If invalid format and checked is false return null, otherwise throw ParseErr.


hash ​

virtual override Int hash()

The hash for a BigInt is platform dependent.


increment ​

@Operator BigInt increment()

Increment by one. Shortcut is ++a or a++.


makeBuf ​

new makeBuf(Buf bytes)

Translates a byte array containing the two's-complement binary representation of a BigInt into a BigInt.


makeInt ​

new makeInt(Int val)

Returns a BigInt whose value is equal to that of the specified Int.


max ​

BigInt max(BigInt that)

Return the larger of this and the specified BigInt values.


min ​

BigInt min(BigInt that)

Return the smaller of this and the specified BigInt values.


minus ​

@Operator BigInt minus(BigInt b)

Subtract b from this. Shortcut is a-b.


minusInt ​

@Operator BigInt minusInt(Int b)

Subtract b from this. Shortcut is a-b.


mod ​

@Operator BigInt mod(BigInt b)

Return remainder of this divided by b. Shortcut is a%b.


modInt ​

@Operator Int modInt(Int b)

Return remainder of this divided by b. Shortcut is a%b.


mult ​

@Operator BigInt mult(BigInt b)

Multiply this with b. Shortcut is a*b.


multInt ​

@Operator BigInt multInt(Int b)

Multiply this with b. Shortcut is a*b.


negate ​

@Operator BigInt negate()

Negative of this. Shortcut is -a.


not ​

BigInt not()

Bitwise not/inverse of this.


one ​

const static BigInt one


or ​

BigInt or(Obj b)

Bitwise-or of this and b.


plus ​

@Operator BigInt plus(BigInt b)

Add this with b. Shortcut is a+b.


plusInt ​

@Operator BigInt plusInt(Int b)

Add this with b. Shortcut is a+b.


pow ​

BigInt pow(Int pow)

Return this value raised to the specified power. Throw ArgErr if pow is less than zero.


setBit ​

BigInt setBit(Int b)

Set the given bit to 1. Equivalent to this.or(1.shiftl(b)).


shiftl ​

BigInt shiftl(Int b)

Bitwise left shift of this by b. Negative values call shiftr instead.


shiftr ​

BigInt shiftr(Int b)

Bitwise arithmetic right-shift of this by b. Note that this is similar to Int.shifta, not Int.shiftr. Sign extension is performed. Negative values call shiftl instead.


signum ​

Int signum()

-1, 0, 1 if the BigInt is negative, zero, or positive.


testBit ​

Bool testBit(Int b)

Return true if given bit is 1. Equivalent to this.and(1.shiftl(b)) != 0.


toBuf ​

Buf toBuf()

Returns a byte array containing the two's-complement representation of this BigInt.


toDecimal ​

Decimal toDecimal()

Convert the number to an Decimal.

This simply wraps the BigInt with Decimal with a 0 scale, equivilent mathematically to int * 2^0


toFloat ​

Float toFloat()

Convert the number to an Float.

If the value is out-of-range, it will return Float.posInf or Float.negInf. Possible loss of precision is still possible, even if the value is finite.


toInt ​

Int toInt(Bool checked := true)

Convert the number to an Int.

If the value is out-of-range and checked is true, an Err is thrown. Otherwise the value is truncated, with possible loss of sign.


toRadix ​

Str toRadix(Int radix, Int? width := null)

Return string representation in given radix. If width is non-null, then leading zeros are prepended to ensure the specified width.


toStr ​

virtual override Str toStr()

Return decimal string representation.


xor ​

BigInt xor(Obj b)

Bitwise-exclusive-or of this and b.


zero ​

const static BigInt zero


Math ​

class Math : Obj

This mixin contains a set of utilities and functions for various math operations.

Methods

matrixCreate a new Matrix with the given number of rows and columns.

Slot Details ​


matrix ​

static Matrix matrix(Int numRows, Int numCols)

Create a new Matrix with the given number of rows and columns. All elements of the matrix are initialized to zero.


Matrix ​

mixin Matrix

Interface for matrix implementations. A matrix is a rectangular array of numbers.

Methods

cofactorCompute the cofactor of the matrix.
determinantCompute the determinant of the matrix.
fillSet every element in the matrix to the given val.
getGet the element at A[i,j], where i is the row index, and j is column index.
inverseCompute the inverse of the matrix.
isSquareReturn true if the matrix is square.
minusComputes A - B and returns a new matrix.
multComputes A * B and returns a new matrix.
multScalarComputes x * A.
numColsThe number of columns in the matrix.
numRowsThe number of rows in the matrix.
plusComputes A + B and returns a new matrix.
setSet the element at A[i,j], where i is the row index, and j is column index.
transposeGet the transpose of the matrix.

Slot Details ​


cofactor ​

abstract Matrix cofactor()

Compute the cofactor of the matrix. The matrix must be square.


determinant ​

abstract Float determinant()

Compute the determinant of the matrix. The matrix must be square.


fill ​

abstract This fill(Float val)

Set every element in the matrix to the given val.


get ​

abstract Float get(Int i, Int j)

Get the element at A[i,j], where i is the row index, and j is column index.


inverse ​

abstract Matrix inverse()

Compute the inverse of the matrix.


isSquare ​

abstract Bool isSquare()

Return true if the matrix is square.


minus ​

@Operator abstract Matrix minus(Matrix b)

Computes A - B and returns a new matrix.


mult ​

@Operator abstract Matrix mult(Matrix b)

Computes A * B and returns a new matrix.


multScalar ​

abstract This multScalar(Float x)

Computes x * A.


numCols ​

abstract Int numCols()

The number of columns in the matrix.


numRows ​

abstract Int numRows()

The number of rows in the matrix.


plus ​

@Operator abstract Matrix plus(Matrix b)

Computes A + B and returns a new matrix.


set ​

abstract This set(Int i, Int j, Float val)

Set the element at A[i,j], where i is the row index, and j is column index.


transpose ​

abstract Matrix transpose()

Get the transpose of the matrix.