Data update
This commit is contained in:
parent
29a5eea0d4
commit
5c1bb7bfa9
2011 changed files with 35081 additions and 3229 deletions
|
|
@ -37,21 +37,22 @@ private:
|
|||
|
||||
class P_adic {
|
||||
public:
|
||||
// Create a P-adic number, with p = 'prime', from the given rational 'numerator' / 'denominator'.
|
||||
// Create a P_adic number, with p = 'prime', from the given rational 'numerator' / 'denominator'.
|
||||
P_adic(const uint32_t& prime, int32_t numerator, int32_t denominator) : prime(prime) {
|
||||
if ( denominator == 0 ) {
|
||||
std::invalid_argument("Denominator cannot be zero");
|
||||
throw std::invalid_argument("Denominator cannot be zero");
|
||||
}
|
||||
|
||||
order = 0;
|
||||
|
||||
// Process rational zero
|
||||
if ( numerator == 0 ) {
|
||||
digits.assign(DIGITS_SIZE, 0);
|
||||
order = ORDER_MAX;
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove multiples of 'prime' and adjust the order of the P-adic number accordingly
|
||||
// Remove multiples of 'prime' and adjust the order of the P_adic number accordingly
|
||||
while ( modulo_prime(numerator) == 0 ) {
|
||||
numerator /= static_cast<int32_t>(prime);
|
||||
order += 1;
|
||||
|
|
@ -62,7 +63,7 @@ public:
|
|||
order -= 1;
|
||||
}
|
||||
|
||||
// Standard calculation of P-adic digits
|
||||
// Standard calculation of P_adic digits
|
||||
const uint64_t inverse = modulo_inverse(denominator);
|
||||
while ( digits.size() < DIGITS_SIZE ) {
|
||||
const uint32_t digit = modulo_prime(numerator * inverse);
|
||||
|
|
@ -85,17 +86,17 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
// Return the sum of this P-adic number with the given P-adic number.
|
||||
// Return the sum of this P_adic number with the given P_adic number.
|
||||
P_adic add(P_adic other) {
|
||||
if ( prime != other.prime ) {
|
||||
std::invalid_argument("Cannot add p-adic's with different primes");
|
||||
throw std::invalid_argument("Cannot add p-adic's with different primes");
|
||||
}
|
||||
|
||||
std::vector<uint32_t> this_digits = digits;
|
||||
std::vector<uint32_t> other_digits = other.digits;
|
||||
std::vector<uint32_t> result;
|
||||
|
||||
// Adjust the digits so that the P-adic points are aligned
|
||||
// Adjust the digits so that the P_adic points are aligned
|
||||
for ( int32_t i = 0; i < -order + other.order; ++i ) {
|
||||
other_digits.insert(other_digits.begin(), 0);
|
||||
}
|
||||
|
|
@ -116,12 +117,12 @@ public:
|
|||
return P_adic(prime, result, all_zero_digits(result) ? ORDER_MAX : std::min(order, other.order));
|
||||
}
|
||||
|
||||
// Return the Rational representation of this P-adic number.
|
||||
// Return the Rational representation of this P_adic number.
|
||||
Rational convert_to_rational() {
|
||||
std::vector<uint32_t> numbers = digits;
|
||||
|
||||
// Zero
|
||||
if ( all_zero_digits(numbers) ) {
|
||||
if ( numbers.empty() || all_zero_digits(numbers) ) {
|
||||
return Rational(1, 0);
|
||||
}
|
||||
|
||||
|
|
@ -171,37 +172,38 @@ public:
|
|||
return Rational(numerator, denominator);
|
||||
}
|
||||
|
||||
// Return a string representation of this p-adic.
|
||||
// Return a string representation of this P_adic number.
|
||||
std::string to_string() {
|
||||
while ( digits.size() > PRECISION ) {
|
||||
digits.pop_back();
|
||||
}
|
||||
pad_with_zeros(digits);
|
||||
std::vector<uint32_t> numbers = digits;
|
||||
pad_with_zeros(numbers);
|
||||
|
||||
std::string result = "";
|
||||
for ( int64_t i = digits.size() - 1; i >= 0; --i ) {
|
||||
for ( int64_t i = numbers.size() - 1; i >= 0; --i ) {
|
||||
result += std::to_string(digits[i]);
|
||||
}
|
||||
|
||||
if ( order >= 0 ) {
|
||||
for ( int32_t i = 0; i < order; ++i ) {
|
||||
result += "0";
|
||||
result.erase(result.begin());
|
||||
}
|
||||
|
||||
result += ".0";
|
||||
} else {
|
||||
result.insert(result.length() + order, ".");
|
||||
|
||||
while ( result[result.length() - 1] == '0' ) {
|
||||
result = result.substr(0, result.length() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
return " ..." + result;
|
||||
return " ..." + result.substr(result.length() - PRECISION - 1);
|
||||
}
|
||||
|
||||
private:
|
||||
/**
|
||||
* Create a P-adic, with p = 'prime', directly from a vector of digits.
|
||||
* Create a P_adic, with p = 'prime', directly from a vector of digits.
|
||||
*
|
||||
* With 'order' = 0, the vector [1, 2, 3, 4, 5] creates the p-adic ...54321.0
|
||||
* For example: with 'order' = 0, the vector [1, 2, 3, 4, 5] creates the p-adic ...54321.0,
|
||||
* 'order' > 0 shifts the vector 'order' places to the left and
|
||||
* 'order' < 0 shifts the vector 'order' places to the right.
|
||||
*/
|
||||
|
|
@ -209,10 +211,10 @@ private:
|
|||
: prime(prime), digits(digits), order(order) {
|
||||
}
|
||||
|
||||
// Transform the given vector of digits representing a p-adic number
|
||||
// into a vector which represents the negation of the p-adic number.
|
||||
void negate_digits(std::vector<uint32_t> numbers) {
|
||||
numbers[0] = ( prime - numbers[0] ) % prime;
|
||||
// Transform the given vector of digits representing a P_adic number
|
||||
// into a vector which represents the negation of the P_adic number.
|
||||
void negate_digits(std::vector<uint32_t>& numbers) {
|
||||
numbers[0] = modulo_prime(prime - numbers[0]);
|
||||
for ( uint64_t i = 1; i < numbers.size(); ++i ) {
|
||||
numbers[i] = prime - 1 - numbers[i];
|
||||
}
|
||||
|
|
@ -221,7 +223,7 @@ private:
|
|||
// Return the multiplicative inverse of the given number modulo 'prime'.
|
||||
uint32_t modulo_inverse(const uint32_t& number) const {
|
||||
uint32_t inverse = 1;
|
||||
while ( ( inverse * number ) % prime != 1 ) {
|
||||
while ( modulo_prime(inverse * number) != 1 ) {
|
||||
inverse += 1;
|
||||
}
|
||||
return inverse;
|
||||
|
|
@ -233,9 +235,9 @@ private:
|
|||
return ( div >= 0 ) ? div : div + prime;
|
||||
}
|
||||
|
||||
// The given vector is padded on the right by zeros up to a maximum length of 'PRECISION'.
|
||||
void pad_with_zeros(std::vector<uint32_t> vector) {
|
||||
while ( vector.size() < PRECISION ) {
|
||||
// The given vector is padded on the right by zeros up to a maximum length of 'DIGITS_SIZE'.
|
||||
void pad_with_zeros(std::vector<uint32_t>& vector) {
|
||||
while ( vector.size() < DIGITS_SIZE ) {
|
||||
vector.emplace_back(0);
|
||||
}
|
||||
}
|
||||
|
|
@ -288,7 +290,7 @@ int main() {
|
|||
std::cout << "4 / 97 => " << padic_two.to_string() << std::endl;
|
||||
|
||||
P_adic sum = padic_one.add(padic_two);
|
||||
std::cout << "sum => " << sum.to_string() << std::endl;
|
||||
std::cout << "sum => " << sum.to_string() << std::endl;
|
||||
std::cout << "Rational = " << sum.convert_to_rational().to_string() << std::endl;
|
||||
std::cout << std::endl;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,8 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public final class PAdicNumbersBasic {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
@ -124,7 +129,7 @@ final class Padic {
|
|||
List<Integer> numbers = new ArrayList<Integer>(digits);
|
||||
|
||||
// Zero
|
||||
if ( allZeroDigits(numbers) ) {
|
||||
if ( numbers.isEmpty() || allZeroDigits(numbers) ) {
|
||||
return new Rational(0, 1);
|
||||
}
|
||||
|
||||
|
|
@ -177,29 +182,28 @@ final class Padic {
|
|||
/**
|
||||
* Return a string representation of this p-adic.
|
||||
*/
|
||||
public String toString() {
|
||||
while ( digits.size() > PRECISION ) {
|
||||
digits.removeLast();
|
||||
}
|
||||
padWithZeros(digits);
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for ( int i = digits.size() - 1; i >= 0; i-- ) {
|
||||
builder.append(digits.get(i));
|
||||
}
|
||||
public String toString() {
|
||||
List<Integer> numbers = new ArrayList<Integer>(digits);
|
||||
padWithZeros(numbers);
|
||||
Collections.reverse(numbers);
|
||||
String numberString = numbers.stream().map(String::valueOf).collect(Collectors.joining());
|
||||
StringBuilder builder = new StringBuilder(numberString);
|
||||
|
||||
if ( order >= 0 ) {
|
||||
for ( int i = 0; i < order; i++ ) {
|
||||
builder.append("0");
|
||||
builder.deleteCharAt(0);
|
||||
}
|
||||
|
||||
builder.append(".0");
|
||||
} else {
|
||||
builder.insert(builder.length() + order, ".");
|
||||
|
||||
while ( builder.toString().endsWith("0") ) {
|
||||
builder.deleteCharAt(builder.length() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
return " ..." + builder.toString();
|
||||
return " ..." + builder.toString().substring(builder.length() - PRECISION - 1);
|
||||
}
|
||||
|
||||
// PRIVATE //
|
||||
|
|
@ -214,7 +218,6 @@ final class Padic {
|
|||
private Padic(int aPrime, List<Integer> aDigits, int aOrder) {
|
||||
prime = aPrime;
|
||||
digits = new ArrayList<Integer>(aDigits);
|
||||
padWithZeros(digits);
|
||||
order = aOrder;
|
||||
}
|
||||
|
||||
|
|
@ -235,7 +238,7 @@ final class Padic {
|
|||
* into a list which represents the negation of the p-adic number.
|
||||
*/
|
||||
private void negateList(List<Integer> aDigits) {
|
||||
aDigits.set(0, ( prime - aDigits.get(0) ) % prime);
|
||||
aDigits.set(0, Math.floorMod(prime - aDigits.get(0), prime));
|
||||
for ( int i = 1; i < aDigits.size(); i++ ) {
|
||||
aDigits.set(i, prime - 1 - aDigits.get(i));
|
||||
}
|
||||
|
|
@ -266,7 +269,7 @@ final class Padic {
|
|||
* The given list is padded on the right by zeros up to a maximum length of 'PRECISION'.
|
||||
*/
|
||||
private static void padWithZeros(List<Integer> aList) {
|
||||
while ( aList.size() < PRECISION ) {
|
||||
while ( aList.size() < DIGITS_SIZE ) {
|
||||
aList.addLast(0);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue