Finding power of exponent without known base in python - math

From an exponent xa, I am trying to get 'a' without a known value of 'x' using logarithm law logx(xa) = a
Originally I tried:
from sympy import *
x = Symbol('x')
print(log(x,x))
Which returns 1 as expected. But when raised to any other power, does not give single value e.g.
print(log(x**2,x))
Which returns:
log(x**2)/log(x)
instead of 2.
Is there any alternative way to get the exponent or fix to my original code?

If you use expand it will give you the result you are looking for. In addition, you can just get the exponent directly:
>>> log(x**2,x).expand() # or expand_log(log(x**2,x))
2
>>> (x**2).exp
2
>>> (x**2).as_base_exp()[1] # works for x**1, too, which is not a Pow

Related

Using python to find the limit of a recusive function

Assume I had the following iterative fuction:
f(z) = z^2 + c
z initally equal to 0
and each answer of the function becomes z for the next iteration. i.e. if c is 1 then the fist iteration gives 1, the second gives 2 and so fourth.
Now assuming I already set a value for c, I would like to be able to use Python to find the limit as this function approaches an infinite number of iterations. How would I best be able to do that? Would Sympy be a good tool?
editied to clearify what I man by iterative function.

ord() Function or ASCII Character Code of String with Z3 Solver

How can I convert a z3.String to a sequence of ASCII values?
For example, here is some code that I thought would check whether the ASCII values of all the characters in the string add up to 100:
import z3
def add_ascii_values(password):
return sum(ord(character) for character in password)
password = z3.String("password")
solver = z3.Solver()
ascii_sum = add_ascii_values(password)
solver.add(ascii_sum == 100)
print(solver.check())
print(solver.model())
Unfortunately, I get this error:
TypeError: ord() expected string of length 1, but SeqRef found
It's apparent that ord doesn't work with z3.String. Is there something in Z3 that does?
The accepted answer dates back to 2018, and things have changed in the mean time which makes the proposed solution no longer work with z3. In particular:
Strings are now formalized by SMTLib. (See https://smtlib.cs.uiowa.edu/theories-UnicodeStrings.shtml)
Unlike the previous version (where strings were simply sequences of bit vectors), strings are now sequences unicode characters. So, the coding used in the previous answer no longer applies.
Based on this, the following would be how this problem would be coded, assuming a password of length 3:
from z3 import *
s = Solver()
# Ord of character at position i
def OrdAt(inp, i):
return StrToCode(SubString(inp, i, 1))
# Adding ascii values for a string of a given length
def add_ascii_values(password, len):
return Sum([OrdAt(password, i) for i in range(len)])
# We'll have to force a constant length
length = 3
password = String("password")
s.add(Length(password) == length)
ascii_sum = add_ascii_values(password, length)
s.add(ascii_sum == 100)
# Also require characters to be printable so we can view them:
for i in range(length):
v = OrdAt(password, i)
s.add(v >= 0x20)
s.add(v <= 0x7E)
print(s.check())
print(s.model()[password])
Note Due to https://github.com/Z3Prover/z3/issues/5773, to be able to run the above, you need a version of z3 that you downloaded on Jan 12, 2022 or afterwards! As of this date, none of the released versions of z3 contain the functions used in this answer.
When run, the above prints:
sat
" #!"
You can check that it satisfies the given constraint, i.e., the ord of characters add up to 100:
>>> sum(ord(c) for c in " #!")
100
Note that we no longer have to worry about modular arithmetic, since OrdAt returns an actual integer, not a bit-vector.
2022 Update
Below answer, written back in 2018, no longer applies; as strings in SMTLib received a major update and thus the code given is outdated. Keeping it here for archival purposes, and in case you happen to have a really old z3 that you cannot upgrade for some reason. See the other answer for a variant that works with the new unicode strings in SMTLib: https://stackoverflow.com/a/70689580/936310
Old Answer from 2018
You're conflating Python strings and Z3 Strings; and unfortunately the two are quite different types.
In Z3py, a String is simply a sequence of 8-bit values. And what you can do with a Z3 is actually quite limited; for instance you cannot iterate over the characters like you did in your add_ascii_values function. See this page for what the allowed functions are: https://rise4fun.com/z3/tutorialcontent/sequences (This page lists the functions in SMTLib parlance; but the equivalent ones are available from the z3py interface.)
There are a few important restrictions/things that you need to keep in mind when working with Z3 sequences and strings:
You have to be very explicit about the lengths; In particular, you cannot sum over strings of arbitrary symbolic length. There are a few things you can do without specifying the length explicitly, but these are limited. (Like regex matches, substring extraction etc.)
You cannot extract a character out of a string. This is an oversight in my opinion, but SMTLib just has no way of doing so for the time being. Instead, you get a list of length 1. This causes a lot of headaches in programming, but there are workarounds. See below.
Anytime you loop over a string/sequence, you have to go up to a fixed bound. There are ways to program so you can cover "all strings upto length N" for some constant "N", but they do get hairy.
Keeping all this in mind, I'd go about coding your example like the following; restricting password to be precisely 10 characters long:
from z3 import *
s = Solver()
# Work around the fact that z3 has no way of giving us an element at an index. Sigh.
ordHelperCounter = 0
def OrdAt(inp, i):
global ordHelperCounter
v = BitVec("OrdAtHelper_%d_%d" % (i, ordHelperCounter), 8)
ordHelperCounter += 1
s.add(Unit(v) == SubString(inp, i, 1))
return v
# Your original function, but note the addition of len parameter and use of Sum
def add_ascii_values(password, len):
return Sum([OrdAt(password, i) for i in range(len)])
# We'll have to force a constant length
length = 10
password = String("password")
s.add(Length(password) == 10)
ascii_sum = add_ascii_values(password, length)
s.add(ascii_sum == 100)
# Also require characters to be printable so we can view them:
for i in range(length):
v = OrdAt(password, i)
s.add(v >= 0x20)
s.add(v <= 0x7E)
print(s.check())
print(s.model()[password])
The OrdAt function works around the problem of not being able to extract characters. Also note how we use Sum instead of sum, and how all "loops" are of fixed iteration count. I also added constraints to make all the ascii codes printable for convenience.
When you run this, you get:
sat
":X|#`y}###"
Let's check it's indeed good:
>>> len(":X|#`y}###")
10
>>> sum(ord(character) for character in ":X|#`y}###")
868
So, we did get a length 10 string; but how come the ord's don't sum up to 100? Now, you have to remember sequences are composed of 8-bit values, and thus the arithmetic is done modulo 256. So, the sum actually is:
>>> sum(ord(character) for character in ":X|#`y}###") % 256
100
To avoid the overflows, you can either use larger bit-vectors, or more simply use Z3's unbounded Integer type Int. To do so, use the BV2Int function, by simply changing add_ascii_values to:
def add_ascii_values(password, len):
return Sum([BV2Int(OrdAt(password, i)) for i in range(len)])
Now we'd get:
unsat
That's because each of our characters has at least value 0x20 and we wanted 10 characters; so there's no way to make them all sum up to 100. And z3 is precisely telling us that. If you increase your sum goal to something more reasonable, you'd start getting proper values.
Programming with z3py is different than regular programming with Python, and z3 String objects are quite different than those of Python itself. Note that the sequence/string logic isn't even standardized yet by the SMTLib folks, so things can change. (In particular, I'm hoping they'll add functionality for extracting elements at an index!).
Having said all this, going over the https://rise4fun.com/z3/tutorialcontent/sequences would be a good start to get familiar with them, and feel free to ask further questions.

About behaviour of / by vector in Julia

3/[2;2] gives
1×2 LinearAlgebra.Transpose{Float64,Array{Float64,1}}:
0.75 0.75
while 3 ./[2;2] gives
2-element Array{Float64,1}:
1.5
1.5
The second one is easy to comprehend. It broadcasts 3 and performs element wise division. But what is the reasoning behind having the first operation behave as it did? I assume it took the sum of the vector, which was 2x1, performed division of 3 by 4 and broadcast it to a 1x2 transposed vector. I can accept taking the sum of the vector to perform division, but why the transpose? Or why not just return a scalar?
It simply gives the right hand side operand's pseudo-inverse.
julia> ?/
...
Right division operator: multiplication of x by the inverse of y on the right.
Although it seems surprising at first sight, it is actually the natural behavior. A rowvector*columnvector gives a scalar and hence a scalar divided by a column vector should give a row vector, which is the case. Note that RowVector has been removed in 1.0 and what you get is actually a row vector represented with Transpose.
You can write #less 1 / [2;2] to see what actually happens.
Also take a look at this GitHub issue to understand the behaviour a bit more and this discourse topic for some use cases.
It seems it is calculating the pseudoinverse of the vector and then multiplying by 3.
Using #which 3/[2;2] and so on to see what actually happens, I found that it is eventually calling the following method in stdlib/LinearAlgebra/generic.jl:
function _vectorpinv(dualfn::Tf, v::AbstractVector{Tv}, tol) where {Tv,Tf}
res = dualfn(similar(v, typeof(zero(Tv) / (abs2(one(Tv)) + abs2(one(Tv))))))
den = sum(abs2, v)
# as tol is the threshold relative to the maximum singular value, for a vector with
# single singular value σ=√den, σ ≦ tol*σ is equivalent to den=0 ∨ tol≥1
if iszero(den) || tol >= one(tol)
fill!(res, zero(eltype(res)))
else
res .= dualfn(v) ./ den
end
return res
end
which in the given case effectively becomes transpose([2;2])/sum(abs2, [2;2]) which is the pseudoinverse.
However, this is a bit above my head. So someone more qualified might prove me wrong.

How can I evaluate an integration to a number?

I have some functions set up like this:
f(x):=1-2**-x$
g(y):=integrate(f(x), x, 0, y)$
and evaluated them:
f(1)$float(%);
g(1)$float(%);
but for g(1), I got a symbolic answer instead of a numerical answer. Using float() was an attempt to get a numerical answer but it just turned all of the terms in the integral into floats.
How can I get g(1) as a number?
Why not just do (by the definition of definite integral):
f(x):=1-2**-x$
gg(x):=''(integrate(f(x), x))$
g(y):=gg(y) - gg(0)$
'' (quote-quote) operator is used to force the evaluation of the :='s right hand side before the assignment.
If you're only interested in a numerical solution, then you could use numerical integration.
For example you could use quad_qag (f(x), x, a, b, key, [epsrel, epsabs, limit]).
I tried:
f(x) := 1-2^(-x);
g(y):= quad_qag(f(x), x, 0, y, 3, epsrel=10d-8)$
g(1);
which returns:
[0.27865247955552,3.093663986714272*10^-15,31,0]
the first entry is the numerical solution,
the second entry is the approximate relative error,
the third entry is the number of iterations required to achieve the solution,
and the last entry is an error code; error codes are
0 if no problems were encountered;
1 if too many sub-intervals were done;
2 if excessive roundoff error is detected;
3 if extremely bad integrand behavior occurs;
6 if the input is invalid.
BTW, the exact solution is 1-1/(2*log(2)) which is approximately 0.27865.

Checking approximation of E

MathWorld page gives a simple numeric formula for e that's allegedly correct for first 10^25 digits. It states that e is approximately
(1 + 9^-4^(7*6))^3^2^85
Any idea how to check whether this formula is correct even for the first 10 digits?
Here's another way of writing the right hand side
Power[Plus[1, Power[9, Times[-1, Power[4, Times[7, 6]]]]], Power[3, Power[2, 85]]]
This problem does not need Mathematica at all. First, it is easy to show that 9^(4^(7*6)) is exactly equal to 3^2^85, since
9^(4^(7*6)) = 3^(2*4^(7*6)) = 3^(2^(1+2*(7*6))) = 3^2^85
Then, we know that one of the ways to represent e is as a limit
e = lim (1+1/n)^n, n->infinity
The only question is what is the error given that n is very large but finite. We have
(1+1/n)^n = e^log((1+1/n)^n) = e^(n*log(1+1/n)) = e^(1-1/(2n)+O(1/n^2)) = e + O(1/n),
Given the n = 3^2^85, i we take the log(10,n) = 2^85 log(10,3) ~ 1.85 *10^25, we get an estimate
similar to the quoted one
Repeatedly taking logs is a nice (usually) generally-applicable solution to problems of this sort. Here's a more special-case approach to this problem: recall that e = lim(n->infinity, (1+1/n)^n). So to be a good approximation to e, all we need is for 9^(4^(42)) (the denominator of the fractional part) to be sufficiently close to 3^(2^85) and big.
In this case, they're identical, so we have n=3^(2^85), and it's going to be a very good approximation to e. These are big numbers, but not unworkably so:
>>> from mpmath import *
>>> iv.dps = 50 # let's use interval arithmetic, just for fun
>>> x = mpi(9)**(-(4**(42)))
>>> up = (mpi(3)**(2**85))
>>> x
mpi('1.4846305545498656772753385085652043615636250118238876e-18457734525360901453873570',
'1.4846305545498656772753385085652043615636250118238899e-18457734525360901453873570')
>>> 1/x
mpi('6.7356824695231749871315222528985858700759934154677854e+18457734525360901453873569',
'6.7356824695231749871315222528985858700759934154678156e+18457734525360901453873569')
>>> up
mpi('6.7356824695231749871315222528985858700759934154678005e+18457734525360901453873569',
'6.7356824695231749871315222528985858700759934154678156e+18457734525360901453873569')
>>> 0 in (1/x-up)
True
Working out the exact error bounds on e is left as an exercise for the reader ;-) -- hint: compare the number of digits of accuracy the mathworld page claims and the above numbers, and ask why that might be, thinking of the series of approximations (1+1/1)^1, (1+1/2)^2, etc.

Resources