Jump to content

P-Adic numbers, basic: Difference between revisions

m
Improved code.
m (Improved coding.)
m (Improved code.)
Line 93:
class P_adic {
public:
// Create a P-adicP_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 ) {
throw std::invalid_argument("Denominator cannot be zero");
}
 
Line 103:
// 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-adicP_adic number accordingly
while ( modulo_prime(numerator) == 0 ) {
numerator /= static_cast<int32_t>(prime);
Line 118 ⟶ 119:
}
 
// Standard calculation of P-adicP_adic digits
const uint64_t inverse = modulo_inverse(denominator);
while ( digits.size() < DIGITS_SIZE ) {
Line 141 ⟶ 142:
}
 
// Return the sum of this P-adicP_adic number with the given P-adicP_adic number.
P_adic add(P_adic other) {
if ( prime != other.prime ) {
throw std::invalid_argument("Cannot add p-adic's with different primes");
}
 
Line 151 ⟶ 152:
std::vector<uint32_t> result;
 
// Adjust the digits so that the P-adicP_adic points are aligned
for ( int32_t i = 0; i < -order + other.order; ++i ) {
other_digits.insert(other_digits.begin(), 0);
Line 172 ⟶ 173:
}
 
// Return the Rational representation of this P-adicP_adic number.
Rational convert_to_rational() {
std::vector<uint32_t> numbers = digits;
Line 227 ⟶ 228:
}
 
// Return a string representation of this p-adicP_adic number.
std::string to_string() {
if ( digits.empty() ) {
digits.assign(DIGITS_SIZE, 0);
}
 
std::vector<uint32_t> numbers = digits;
pad_with_zeros(numbers);
Line 244 ⟶ 241:
for ( int32_t i = 0; i < order; ++i ) {
result += "0";
result.erase(result.begin());
}
 
Line 250 ⟶ 246:
} else {
result.insert(result.length() + order, ".");
 
result = "0" + result;
while ( result[result.length() - 1] == '0' ) {
result = result.erasesubstr(0, result.beginlength() - 1);
}
}
 
constreturn uint32_t" surplus..." =+ DIGITS_SIZEresult.substr(result.length() - PRECISION - 1);
return " ..." + result.substr(surplus, result.length() - surplus);
}
 
private:
/**
* Create a P-adicP_adic, with p = 'prime', directly from a vector of digits.
*
* WithFor 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.
Line 269 ⟶ 267:
}
 
// Transform the given vector of digits representing a p-adicP_adic number
// into a vector which represents the negation of the p-adicP_adic number.
void negate_digits(std::vector<uint32_t>& numbers) {
numbers[0] = modulo_prime( prime - numbers[0] ) % prime;
for ( uint64_t i = 1; i < numbers.size(); ++i ) {
numbers[i] = prime - 1 - numbers[i];
Line 294 ⟶ 292:
 
// 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);
Line 366 ⟶ 364:
{{ out }}
<pre>
</3-adic numbers:
-2 / 87 => ...2101020111222001212021110002210102011122101020111222001212021110002210102011122.2
4 / 97 => ...1022220111100202001010001200002111122021022220111100202001010001200002111122021.0
sum => ...0201011000022210220101111202212220210220201011000022210220101111202212220210220.2
Rational = 154 / 8439
 
7-adic numbers:
5 / 8 => ...2424242424242424242424242424242424242425424242424242424242424242424242424242425.0
353 / 30809 => ...1560462505550343461155520004023663643455560462505550343461155520004023663643455.0
sum => ...4315035233123101033613062431266421216213315035233123101033613062431266421216213.0
Rational = 156869 / 246472
</pre>
897

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.