0

I have a MATLAB function :

Bits=30
NBits= ceil(fzero(@(x)2^(x) - x -1 - Bits, max(log2(Bits),1)))

I want to convert it to python, I wrote something like this so far:

from numpy import log, log2
from scipy.optimize import root_scalar

def func(x,Bits):
    return ((x)2^(x)-x-1-Bits, max(log2(Bits)))

However it says that it need to be (x)*2^ Does anybody know first, if the conversion from Matlab to python is correct? and second if * has to be added?

Upon suggestion I wrote this lambda function:

lambda x: (2^(x) -x -1 -Bits) , max(log2(Bits))

but I get this error: TypeError: 'numpy.float64' object is not iterable

13
  • 1
    The python syntax for "to the power of" is ** rather than ^. So to raise something to the power of 2 you would do x**2. Commented Dec 16, 2020 at 14:21
  • 1
    Note that the @(x) in your MATLAB code indicates an anonymous function. In Python this can be created using lambda Commented Dec 16, 2020 at 14:24
  • oh right, so I just need to use the lambda so I apply it to the second argument in this case Commented Dec 16, 2020 at 14:26
  • I think there was some mistake in the original code, there should be a space Commented Dec 16, 2020 at 14:27
  • ceil(fzero(@(x) 2^(x) - x -1 - numDataBits, max(log2(numDataBits),1))) Commented Dec 16, 2020 at 14:27

1 Answer 1

2

I don't have numpy or scipy on this computer so here is my best attempt at an answer.

def YourFunc(Bits):
    return math.ceil(root_scalar(lambda x: (2**x)-x-1-Bits, x0 = max(log2(Bits),1)))

Bits = 30
NBits = YourFunc(30)

print(NBits)

I used this function for log2 rather than the one from numpy. Try it

def log2(x):
    return math.log(x,2)
Sign up to request clarification or add additional context in comments.

9 Comments

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in YourFunc File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/scipy/optimize/_root_scalar.py", line 230, in root_scalar raise ValueError('Unable to select a solver as neither bracket ' ValueError: Unable to select a solver as neither bracket nor starting point provided.
I think we are on the right path but I got this error now after importing root_scalar
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in YourFunc File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/scipy/optimize/_root_scalar.py", line 251, in root_scalar raise ValueError('x1 must not be None for %s' % method) ValueError: x1 must not be None for secant
sorry this now after I used the edited function with only one argument, without x, yeah no need to specify x
@PaoloLorenzini I don't know how the root_scalar function works I'm afraid. I'd try either adding x1=1 as a third argument to root_scalar or changing the solver method method='bisect' or one of the other methods list in the documentation.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.