# Element-wise ("vectorial") operation in SageMath

Thanks to this forum, I got running some lines of code in SageMath as follows:

def y(x): return RR(sum(map(log, prime_range(1,x))))
y(20)
16.0876044842000


It takes the log of all primes less than a real value of x and adds them.

Now I'm looking at dividing the log(prime) by that prime before adding them up. Actually, not even adding them up.

So it seems like a reasonable first step to take out the sum() from the function, but this does not work so well:

def y(x): return RR(map(log, prime_range(1,x)))
y(20)


yielding an TypeError: unable to convert '<mapobjectat0x7f7d91273250>' to a real number despite leaving RR or permuting its position within the function.

I have not even attempted the second logical step of dividing element-wise by prime_range(1,x).

Example:

For y(4) it would be

log(2) / 2 + log(3) /3

edit retag close merge delete

Sort by » oldest newest most voted The symbolic sum:

sage: def y_symbolic(x): return sum(log(p)/p for p in prime_range(1,x))
sage: y_symbolic(4)
1/3*log(3) + 1/2*log(2)
sage: RR(y_symbolic(4))
0.712777686502676


The sum of reals:

sage: def y_real(x): return sum(log(RR(p))/p for p in prime_range(1,x))
sage: y_real(4)
0.712777686502676


The individual terms in the real sum:

sage: def y_real_terms(x): return [log(RR(p))/p for p in prime_range(1,x)]
sage: y_real_terms(4)
[0.346573590279973, 0.366204096222703]


etc.

more