package maths import ( "golang.org/x/exp/constraints" ) // IsPrime checks if the given number is prime. // It returns true if the number is prime, and false otherwise. func IsPrime[T constraints.Integer](n T) bool { if n <= 1 { return false } if n <= 3 { return true } if n%2 == 0 || n%3 == 0 { return false } for i := T(5); i*i <= n; i += 6 { if n%i == 0 || n%(i+2) == 0 { return false } } return true } // IntPow calculates the power of an integer. // It takes two parameters, x and y, and returns the result of x raised to the power of y. // The type of x and y must be an integer type. func IntPow[T constraints.Integer](x, y T) T { result := T(1) for i := T(0); i < y; i++ { result *= x } return result } // Pow2 calculates the power of 2 for the given integer value. // It performs a bitwise left shift operation on the number 1 by the value of x. // The result is returned as an integer of the same type as the input. func Pow2[T constraints.Integer](x T) T { return 1 << x } // GCD calculates the greatest common divisor (GCD) of two integers. // It uses the Euclidean algorithm to find the GCD. // The function takes two parameters, 'a' and 'b', which represent the integers. // It returns the GCD as the result. func GCD[T constraints.Integer](a, b T) T { for b != 0 { t := b b = a % b a = t } return a } // LCM calculates the least common multiple (LCM) of two or more integers. // It takes two integers, a and b, as the first two arguments, and an optional variadic parameter integers for additional integers. // The function uses the GCD (greatest common divisor) function to calculate the LCM. // It returns the LCM as the result. func LCM[T constraints.Integer](a, b T, integers ...T) T { result := a * b / GCD(a, b) for i := 0; i < len(integers); i++ { result = LCM(result, integers[i]) } return result } // AbsInt returns the absolute value of an integer. // It takes an integer value x as input and returns the absolute value of x. func AbsInt[T constraints.Integer](x T) T { if x >= 0 { return x } else { return -x } } // gaussianElimination performs Gaussian elimination on the given coefficients matrix and right-hand side vector. // It solves a system of linear equations by transforming the coefficients matrix into row-echelon form. // The function takes a square matrix of type Matrix[float64] and a vector of type []float64 as input. // The size of the matrix and the length of the vector should be the same. // The function modifies the coefficients matrix and the right-hand side vector in place. // The solution is stored into the rhs vector. func gaussianElimination(coefficients Matrix[float64], rhs []float64) { size := len(coefficients) for i := 0; i < size; i++ { // Select pivot pivot := coefficients[i][i] // Normalize row i for j := 0; j < size; j++ { coefficients[i][j] = coefficients[i][j] / pivot } rhs[i] = rhs[i] / pivot // Sweep using row i for k := 0; k < size; k++ { if k != i { factor := coefficients[k][i] for j := 0; j < size; j++ { coefficients[k][j] = coefficients[k][j] - factor*coefficients[i][j] } rhs[k] = rhs[k] - factor*rhs[i] } } } } func MaxInteger[T constraints.Integer]() T { return T(^uint(T(0)) >> 1) }