Ask Your Question
1

Element-wise ("vectorial") operation in SageMath

asked 2023-05-23 17:57:58 +0200

toni gravatar image

updated 2023-05-23 18:54:18 +0200

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-23 19:11:21 +0200

rburing gravatar image

updated 2023-05-23 19:13:42 +0200

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-05-23 17:57:58 +0200

Seen: 212 times

Last updated: May 23 '23