Convert phyton code into Matlab code
Mostrar comentarios más antiguos
#'''
################################################################
# #
# Conversion of the decimal number to the number of radix r #
# #
################################################################
# To print intermediate values set prt = 1
# To restrict to print intermediate values set prt to any number
# Parameters of the conversion
prt = 1 # Print intermediate results
#prt = 0 # If any number, the the program does not print intermediate results
numb = 13. # Number to be converted
r = 2 # radix of the target number
epsilon = 10e-5 # minimum value of the absolute precision of the target number
# Calling the function to convert decimal number numb to the number with radix r
cv.Dec_to_x_r(numb, r, epsilon, prt) # calling the function Dec_to_x_r(numb, r, epsilon, prt) to convert the numbers
################################################################
# #
# Conversion of the number x_r of the radix r to decimal #
# #
################################################################
# To print intermediate values set prt = 1
# To restrict to print intermediate values set prt to any number
# Parameters of the conversion
prt = 1 # Print intermediate results
#prt = 0 # If any number, the the program does not print intermediate results
# Parameters of the conversion
x_r = '1AC.B92'; r = 16
#x_r = '1001011.0101' # Number to be converted
#r = 4 # radix of the number to be converteed
# Calling the function to convert the nnumber x_r
# with radix r to the decimal
cv.Xr_to_dec(x_r, r, prt) # calling the function Xr_to_dec(x_r, r, prt) to convert the numbers
#'''
________________________________________________________________________________________________________________________
import numpy as np
import sys
def Xr_to_dec(x_r, r, prt):
'''
Functionn converts a number 'x_r' of radix 'r' into the decimal number
'x_r', string, is a number of radix r
'r', integer, is a radix
'''
if ('.' in x_r):
radix_ind = x_r.find('.') # Returns index of radix '.'
int_part = x_r[0:radix_ind] # Returns integer part as a string of 'x_r'
frac_part = x_r[radix_ind + 1: ] # Returns fractional part as a string of 'x_r'
else:
int_part = x_r
frac_part = ''
for k in int_part:
if int(float(str_to_numb(k))) >= r:
print('A numeral ({:s})_{:d} of imput number {:s} equals or is bigger\
\nthan the radix r = {:d}: ({:s})_{:d} >= {:d}'.format(k, r, x_r, r, k, r, r))
print('{:s}'.format('The program is terminated!'))
sys.exit()
for k in frac_part:
if int(float(str_to_numb(k))) >= r:
print('A numeral ({:s})_{:d} of imput number {:s} equals or is bigger\
\nthan the radix r = {:d}: ({:s})_{:d} >= {:d}'.format(k, r, x_r, r, k, r, r))
print('{:s}'.format('The program is terminated!'))
sys.exit()
#print(radix_ind)
# Conversion of the integer part
y_int = 0.0
sk = len(int_part)
count = 1 # number of iteration
for k in int_part:
num = str_to_numb(k) # Returns numerical string of a numeral 'k' of 'int_part'
num_int = int(float(num)) # Returns Integer of numerical string 'num'
y_int = y_int + num_int * r**(sk - count)
count = count + 1
# Conversion of the fractional part
y_frac = 0.0
sk = len(frac_part)
count = 1 # number of iteration
for k in frac_part:
num = str_to_numb(k) # Returns numerical string of a numeral 'k' of 'frac_part'
num_int = int(float(num)) # Returns Integer of numerical string 'num'
y_frac = y_frac + num_int * r**(-count)
count = count + 1
y = y_int + y_frac
if prt == 1:
print('\n\n{:^60s}'.format('Result of the conversion to the decimal number'))
print('{:s}'.format(60*'-'))
print('{:s} ({:s})_{:d}\nof radix r={:d} to the decimal'.\
format('Rezult of the conversion of number', x_r, r, r))
print('({:s})_{:d} = ({:f})_10.'.format(x_r, r, y))
print('{:s}'.format(60*'-'))
return(y)
def Dec_to_x_r(x, r, epsilon, prt):
'''
Functionn converts a decimal number 'x' of radix 10 into the number of radix r
'x', string, is a decimal number
'r', integer, is a radix of the number y_r that is a result of the conversion of the decimal number x
'epsilon' is minimum value of the absolute precission: epsilon = 1/(r^n),
where 'r' is a radix, 'n' is number of significant digits
after the radix point
'''
x = str(x)
if ('.' in x):
radix_ind = x.find('.') # Returns index of radix '.'
int_part = x[0:radix_ind] # Returns integer part as a string of 'x_r'
frac_part = x[radix_ind:] # Returns fractional part as a string of 'x_r'
else:
int_part = x
frac_part = ''
#print(int_part)
#print(frac_part)
#########################################################
# Conversion of the integer part of the decimal number
# x into the number y_r whose radix is r
#########################################################
y_v = [] # empty list of numerals of the remainders
count = 1 # number of iteration
# The loop while integer part of the quotient is bigger than 0
#prt = 1
if prt == 1:
print('Conversion of decimal number ({:s})_10 to number X_{:d} of radix r={:d}'.format(x, r, r))
print('{:<50s}'.format(50*'-'))
print('{:^50s}'.format('Intermediate results of conversion'))
print('{:^50s}'.format('of the integer part of the decimal number'))
print('{:^50s}'.format('into a target number of radix r: ' + str(r)))
print('Number: ({:s})_10\
\nInteger part: ({:s})_10\
\nRadix of the target number: r = {:d}'\
.format(x, int_part, r))
l = max(7, len(int_part))
l_1 = max(7, len(str(int(float(int_part)/r))))
a1 = str(4); a2 = str(l+8); a3 = str(l_1); a4 = str(4); a5 = str(12)
s1 = '{:^'+a1+'s}|'+'{:^'+a2+'s}|'+'{:^'+a3+'s}|'+'{:^'+a4+'s}|'+'{:'+a5+'s}|'
str1 = s1.format(' ', 'Operation', 'Integer', 'Re-', 'Converted ')
str2 = s1.format('No.', 'of integer', 'part', 'mai-', 'Number')
str3 = s1.format(' ', 'division', ' ', 'nder', 'of radix r')
# Printing of the head of the table
print('{:s}'.format(len(str1)*'-'))
print(str1)
print(str2)
print(str3)
print('{:s}'.format(len(str1)*'-'))
quotient_int = float(int_part)
while quotient_int > 0:
quotient_int_old = quotient_int
quotient_int = int(quotient_int / r)
remainder = int(quotient_int_old - quotient_int * r)
y_v.append(remainder)
count = count + 1
if count > 100: break
###########################################################
# Printing the intermediate results of the coonversations
##########################################################
if prt == 1:
#l = max(12, len(int_part)) # length of the integer part
#l_1 = len(str(int(float(int_part)/r)))
y_intermed = list(reversed(y_v))
c = ''
for k in y_intermed: c = c + numb_to_str(str(k))
d = '{:4d}|' + '{:>' + str(l+1) + 'd}' + ' : {:2d} = ' + '{:' + \
str(max(7, l_1)) + 'd}' + '|{:>4d}| {:s}'
print(d.format(count-1, int(quotient_int_old), int(r), quotient_int, remainder, c))
y_v = list(reversed(y_v))
# A integer part of the converted decimal number
y_r_int = ''
for k in y_v:
k = numb_to_str(str(k))
y_r_int = y_r_int + k
##########################################################
# Conversion of the fractional part of the decimal number
# frac(x_10) into the fractional part frac(y_r) whose
# radix is r with required minimum value o the absolute
#precission epsilon
##########################################################
# Required number of significant digits after the radix point
n = 1 + int(np.log(1./epsilon)/np.log(r))
if prt == 1:
print('{:<50s}'.format(50*'-'))
print('{:^50s}'.format('Intermediate results of conversion'))
print('{:^50s}'.format('of the fractional part of the decimal number'))
print('{:^50s}'.format('into a target number of radix r: ' + str(r)))
print('Number: ({:s})_10\
\nFractional part: ({:s})_10\
\nRadix of the target number r = {:d}'\
.format(x, frac_part, r))
print('Minimum value of the absolute precision\nof the target number epsilon: {:.3e}\
\nRequired number of significant digits n = {:d}'\
.format(epsilon, n ))
l = max(7, len(frac_part))
l_1 = 12 #min(12, max(7, len(frac_part * r)))
a1 = str(4); a2 = str(l+8); a3 = str(l_1); a4 = str(6); a5 = str(12)
s1 = '{:^'+a1+'s}|'+'{:^'+a2+'s}|'+'{:^'+a3+'s}|'+'{:^'+a4+'s}|'+'{:'+a5+'s}|'
str1 = s1.format(' ', 'Operation', ' ', 'Inte-', 'Converted ')
str2 = s1.format('No.', 'of multi-', 'Rezult', 'ger ', 'number')
str3 = s1.format(' ', 'plication', ' ', 'part ', 'of radix r')
# Printing of the head of the table
print('{:s}'.format(len(str1)*'-'))
print(str1)
print(str2)
print(str3)
print('{:s}'.format(len(str1)*'-'))
y_app_str = '.'
factor = float(frac_part)
count = 1
for k in range(1, n+1):
factor_old = factor
factor = factor * r
factor_old_1 = factor
factor_int = int(factor) # integer part of the factor
factor_int_num = factor_int
factor = factor - factor_int
factor_int = numb_to_str(str(factor_int))
y_app_str = y_app_str + factor_int
#'''
if prt == 1:
#l = len(frac_part) # length of the fractional part
#l_1 = min(7, len(frac_part * r))
d = '{:4d}|' + ' {:' + str((l)) + 'f}' + \
' * {:2d} = ' + \
'{:<11.7f}| {:5d}| {:s}'
print(d.format(count, factor_old, r, factor_old_1, factor_int_num, y_app_str))
#'''
count = count + 1
if factor == 0: break
y_r = y_r_int + y_app_str
if prt == 1:
print('{:<50s}'.format(50*'-'))
print('{:<50s}'.format('Results of conversion:'))
print('({:s})_10 = ({:s})_{:d} '.format(x, y_r, r))
print('{:<50s}'.format(50*'-'))
return(y_r)
def str_to_numb(x):
'''
The function returns the decimal number
that corrsponds to the alphabet letters A, B, C and so on
if x \in {A, B, C, ... O}
x, string, is a letter of English (Latin) alphabet A, B and so on or numeral 1, 2, 3, ..., 9
'''
if ord(x) >= 65:
return str(ord(x) - 55)
else:
return str(x)
def rel_abs_err_10(x, y):
'''
Function returns the relative absolute decimal error
(x - y)/x
y - a quantity to be measured
x - an 'exact' value
'''
return((x - y)/x)
def numb_to_str(x):
'''
The function returns the alphabet letters A, B, C and so on that corresponds to the decimal numbers bigger than 9
x, string, is a number that is assignet to the
English (Latin) alphabet letter A, B and so on if x >= 11
'''
if int(float(x))>=10:
return chr(int(float(x)) + 55)
else:
return str(x)
if __name__ == '__main__':
################################################################
# #
# Conversion of the decimal number to the number of radix r #
# #
################################################################
# To print intermediate values set prt = 1
# To restrict to print intermediate values set prt to any number
# Parameters of the conversion
prt = 1 # Print intermediate results
#prt = 0 # If any number, the the program does not print intermediate results
numb = 23.23 # Number to be converted
r = 2 # radix of the target number
epsilon = 1e-4 # minimum value of the absolute precision of the target number
# Calling the function to convert decimal number numb to the number with radix r
cv.Dec_to_x_r(numb, r, epsilon, prt)
################################################################
# #
# Conversion of the number x_r of the radix r to decimal #
# #
################################################################
# To print intermediate values set prt = 1
# To restrict to print intermediate values set prt to any number
# Parameters of the conversion
prt = 1 # Print intermediate results
#prt = 0 # If any number, the the program does not print intermediate results
# Parameters of the conversion
x_r = '70007.0207' # Number to be converted
r = 8 # radix of the number to be converteed
# Calling the function to convert the nnumber x_r
# with radix r to the decimal
cv.Xr_to_dec(x_r, r, prt)
#'''
xxx = [['1001.101', 2], ['11101111.101111', 2],
['22212.121212', 3], ['20120.102', 3],
['322102.132313', 4], ['203123.032', 4],
['370076.0011', 8], ['70620.12032', 8],
['7256.11', 8], ['1102.120', 8],
['AB01D.F1E11', 16], ['17FA.FF5', 16],
['1.011', 16], ['201A.1FE', 16],
['100.0', 16], ['2.0', 16],
['5.1', 16], ['9.9', 16],
]
for k in xxx:
x_r = k[0]
r = k[1]
#print(x_r, r)
cv.Xr_to_dec(x_r, r, prt)
Respuestas (1)
Image Analyst
el 12 de En. de 2019
0 votos
I don't know of any utility to do it, so if you don't want to go through it line-by-line and do it yourself, the Mathworks can do it for you. Click this link: MathWorks Consulting
Categorías
Más información sobre Call Python from MATLAB en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!