Adding addition and substraction (partial) functions

This commit is contained in:
chabisik 2022-03-26 00:30:09 +01:00
parent 231f22b48d
commit f84cdbc2b3
2 changed files with 138 additions and 116 deletions

View file

@ -4,125 +4,138 @@ import sys
import numpy
import time
def apply_codes_for_coding(arg1, arg2):
arg1_coded_vector = numpy.array([arg1, arg1]) #vector obtained when multipying arg1 by code [1 1]
arg2_coded_vector = numpy.array([arg2, -arg2]) #vector obtained when multipying arg2 by code [1 -1]
result_vector = (arg1_coded_vector + arg2_coded_vector)/2.0
return result_vector[0], result_vector[1]
def overlap(arg1, arg2, divide_overlap_result_by=1.0):
overlap_result = ""
def sb_addition(arg1: str, arg2: str) -> str: #will do both addition and substraction
result = ''
# transform arg1 to string if not already in string and test if it's a full integer (without decimal part or decimal part equals zero)
arg1_as_string = str(arg1)
arg1_integer_part = arg1_as_string
arg1_decimal_part = ''
arg1_is_full_integer = True
arg1_is_negative = True if arg1_integer_part.startswith('-') else False
try:
arg1_integer_part, arg1_decimal_part = arg1_as_string.split(".")
if arg1_decimal_part==len(arg1_decimal_part)*'0': arg1_is_full_integer = True
else: arg1_is_full_integer = False
except:
pass#nothing to do
if arg1_is_negative: arg1_integer_part = arg1_integer_part.replace('-', '')
# transform arg2 to string if not already in string and test if it's a full integer (without decimal part or decimal part equals zero)
arg2_as_string = str(arg2)
#print("arg1", arg1_as_string, "arg2", arg2_as_string)
arg1_integer_part, arg1_decimal_part = arg1_as_string.split(".")
arg2_integer_part, arg2_decimal_part = arg2_as_string.split(".")
#print("arg1 : interger part", arg1_integer_part, "and decimal part", arg1_decimal_part)
#print("arg2 : interger part", arg2_integer_part, "and decimal part", arg2_decimal_part)
#print("===============================================")
if not(arg1_integer_part.startswith("-")) and arg2_integer_part.startswith("-"): #arg1 positive - arg2 negative
arg2_integer_part = arg2_integer_part.replace("-", "")
overlap_result += "1"
elif not(arg1_integer_part.startswith("-")) and not(arg2_integer_part.startswith("-")): #arg1 positive - arg2 positive
overlap_result += "2"
elif arg1_integer_part.startswith("-") and not(arg2_integer_part.startswith("-")): #arg1 negative - arg2 positive
arg1_interger_part = arg1_integer_part.replace("-", "")
overlap_result += "3"
elif arg1_integer_part.startswith("-") and arg2_integer_part.startswith("-"): #arg1 negative - arg2 negative
arg1_integer_part = arg1_integer_part.replace("-", "")
arg2_integer_part = arg2_integer_part.replace("-", "")
overlap_result += "4"
#print("arg1 : interger part", arg1_integer_part, "and decimal part", arg1_decimal_part)
#print("arg2 : interger part", arg2_integer_part, "and decimal part", arg2_decimal_part)
#print("overlap start", overlap_result)
#print("===============================================")
max_integer_part_size = max(len(arg1_integer_part),len(arg2_integer_part))
arg2_integer_part = arg2_as_string
arg2_decimal_part = ''
arg2_is_full_integer = True
arg2_is_negative = True if arg2_integer_part.startswith('-') else False
try:
arg2_integer_part, arg2_decimal_part = arg2_as_string.split(".")
if arg2_decimal_part==len(arg2_decimal_part)*'0': arg2_is_full_integer = True
else: arg2_is_full_integer = False
except:
pass#nothing to do
if arg2_is_negative: arg2_integer_part = arg2_integer_part.replace('-', '')
# making integer and decimal parts same size
max_integer_part_size = max( len(arg1_integer_part), len(arg2_integer_part) )
arg1_integer_part = '0'*(max_integer_part_size-len(arg1_integer_part)) + arg1_integer_part
arg2_integer_part = '0'*(max_integer_part_size-len(arg2_integer_part)) + arg2_integer_part
max_decimal_part_size = max(len(arg1_decimal_part),len(arg2_decimal_part))
arg1_integer_part = "0"*(max_integer_part_size-len(arg1_integer_part)) + arg1_integer_part
arg2_integer_part = "0"*(max_integer_part_size-len(arg2_integer_part)) + arg2_integer_part
arg1_decimal_part = arg1_decimal_part + "0"*(max_decimal_part_size-len(arg1_decimal_part))
arg2_decimal_part = arg2_decimal_part + "0"*(max_decimal_part_size-len(arg2_decimal_part))
#print("arg1 : interger part", arg1_integer_part, "and decimal part", arg1_decimal_part)
#print("arg2 : interger part", arg2_integer_part, "and decimal part", arg2_decimal_part)
#print("===============================================")
for index in range(max_integer_part_size):
overlap_result += arg1_integer_part[index]
overlap_result += arg2_integer_part[index]
overlap_result += "."
for index in range(max_decimal_part_size):
overlap_result += arg1_decimal_part[index]
overlap_result += arg2_decimal_part[index]
#print("string overlap start", overlap_result, "string size in memory", sys.getsizeof(overlap_result))
#print("float overlap start", float(overlap_result), "float size in memory", sys.getsizeof(float(overlap_result)))
return float(overlap_result)/float(divide_overlap_result_by)
def unoverlap(arg, before_unoverlap_multiply_by=1.0):
arg1 = "" #for stocking unoverlapped values
arg2 = ""
arg_as_string = str(arg*before_unoverlap_multiply_by)
arg_integer_part, arg_decimal_part = arg_as_string.split(".")
if arg_integer_part[0]=="1": #arg1 must be positive - arg2 must be negative
arg2 += "-"
elif arg_integer_part[0]=="2": #arg1 must be positive - arg2 must be positive
arg1_decimal_part = arg1_decimal_part + '0'*(max_decimal_part_size-len(arg1_decimal_part))
arg2_decimal_part = arg2_decimal_part + '0'*(max_decimal_part_size-len(arg2_decimal_part))
print("arg1_integer_part", arg1_integer_part)
print("arg1_decimal_part", arg1_decimal_part)
print("arg2_integer_part", arg2_integer_part)
print("arg2_decimal_part", arg2_decimal_part)
# making real addition operation
rem = '0'
if (not(arg1_is_negative) and not(arg2_is_negative)) or (arg1_is_negative and arg2_is_negative): # both numbers are positives OR both numbers are negatives
if not(arg1_is_full_integer) or not(arg2_is_full_integer):
for i in range(max_decimal_part_size-1, -1, -1):
res = int( eval('(' + arg1_decimal_part[i] + '+' + arg2_decimal_part[i] + '+' + str(rem) + ')%10') )
result = str(res) + result
rem = int( eval('((' + arg1_decimal_part[i] + '+' + arg2_decimal_part[i] + '+' + str(rem) + ') - ' + str(res) + ')/10') )
print("rem", rem)
result = '.' + result
for i in range(max_integer_part_size-1, -1, -1):
res = int( eval('(' + arg1_integer_part[i] + '+' + arg2_integer_part[i] + '+' + str(rem) + ')%10') )
result = str(res) + result
rem = int( eval('((' + arg1_integer_part[i] + '+' + arg2_integer_part[i] + '+' + str(rem) + ') - ' + str(res) + ')/10') )
print("rem", rem)
if str(rem) != '0': result = str(rem) + result
if arg1_is_negative and arg2_is_negative: result = '-' + result # treat the case both numbers are negative
elif not(arg1_is_negative) and arg2_is_negative: # first number is positive and second one is negative
pass
elif arg_integer_part[0]=="3": #arg1 must be negative - arg2 must be positive
arg1 += "-"
elif arg_integer_part[0]=="4": #arg1 must be negative - arg2 must be negative
arg1 += "-"
arg2 += "-"
arg_integer_part = arg_integer_part[1:]
for index in range(len(arg_integer_part)):
if index%2==0: arg1 += arg_integer_part[index]
#arg1 += arg_integer_part[index]
else: arg2 += arg_integer_part[index]
#arg2 += arg_integer_part[index]
arg1 += "."
arg2 += "."
for index in range(len(arg_decimal_part)):
if index%2==0:
arg1 += arg_decimal_part[index]
else:
arg2 += arg_decimal_part[index]
#print("arg1", arg1, "arg2", arg2)
return float(arg1), float(arg2)
elif arg1_is_negative and not(arg2_is_negative): # first number is negative and second one is positive
pass
print("result now:", result)
return result
def apply_codes_for_decoding(arg1, arg2):
coded_vector = numpy.array([arg1, -arg2])
code_one = numpy.array([1.0, 1.0])
code_two = numpy.array([1.0, -1.0])
return numpy.sum(coded_vector*code_one), numpy.sum(coded_vector*code_two)
def sb_substract(arg1: str, arg2: str, are_already_same_size: bool=False) -> str:
result = '' # will be arg1-arg2
# transform arg1 to string if not already in string and test if it's a full integer (without decimal part or decimal part equals zero)
arg1_as_string = str(arg1)
arg1_integer_part = arg1_as_string
arg1_decimal_part = ''
arg1_is_full_integer = True
arg1_is_negative = True if arg1_integer_part.startswith('-') else False
try:
arg1_integer_part, arg1_decimal_part = arg1_as_string.split(".")
if arg1_decimal_part==len(arg1_decimal_part)*'0': arg1_is_full_integer = True
else: arg1_is_full_integer = False
except:
pass#nothing to do
if arg1_is_negative: arg1_integer_part = arg1_integer_part.replace('-', '')
# transform arg2 to string if not already in string and test if it's a full integer (without decimal part or decimal part equals zero)
arg2_as_string = str(arg2)
arg2_integer_part = arg2_as_string
arg2_decimal_part = ''
arg2_is_full_integer = True
arg2_is_negative = True if arg2_integer_part.startswith('-') else False
try:
arg2_integer_part, arg2_decimal_part = arg2_as_string.split(".")
if arg2_decimal_part==len(arg2_decimal_part)*'0': arg2_is_full_integer = True
else: arg2_is_full_integer = False
except:
pass#nothing to do
if arg2_is_negative: arg2_integer_part = arg2_integer_part.replace('-', '')
if not(are_already_same_size):
# making integer and decimal parts same size
max_integer_part_size = max( len(arg1_integer_part), len(arg2_integer_part) )
arg1_integer_part = '0'*(max_integer_part_size-len(arg1_integer_part)) + arg1_integer_part
arg2_integer_part = '0'*(max_integer_part_size-len(arg2_integer_part)) + arg2_integer_part
max_decimal_part_size = max(len(arg1_decimal_part),len(arg2_decimal_part))
arg1_decimal_part = arg1_decimal_part + '0'*(max_decimal_part_size-len(arg1_decimal_part))
arg2_decimal_part = arg2_decimal_part + '0'*(max_decimal_part_size-len(arg2_decimal_part))
# making real substraction operation
if arg1_is_negative and not(arg2_is_negative): # first number is negative and second one is positive
return sb_addition('-'+arg1_integer_part+'.'+arg1_decimal_part, '-'+arg2_integer_part+'.'+arg2_decimal_part) # second number will become negative when doing arg1-arg2
if not(arg1_is_negative) and arg2_is_negative: # first number is positive and second one is negative
return sb_addition(arg1_integer_part+'.'+arg1_decimal_part, arg2_integer_part+'.'+arg2_decimal_part) # second number will become positive when doing arg1-arg2
# when not in one of the above cases, substraction operation can be considered as not being a simple addition
if arg1_is_negative and arg2_is_negative: # both numbers are negatives
# then (-arg1)-(-arg2) becomes arg2-arg1
temp = arg1_integer_part
arg1_integer_part = arg2_integer_part
arg2_integer_part = temp
# --------------
temp = arg1_decimal_part
arg1_decimal_part = arg2_decimal_part
arg2_decimal_part = temp
rem = '0'
print("arg1_integer_part", arg1_integer_part, "arg1_decimal_part", arg1_decimal_part)
print("arg2_integer_part", arg2_integer_part, "arg2_decimal_part", arg2_decimal_part)
if not(arg1_is_full_integer) or not(arg2_is_full_integer):
for i in range(max_decimal_part_size-1, -1, -1):
upper_digit_to_consider = int( eval( arg1_decimal_part[i] + '+' + '10 if ' + arg1_decimal_part[i] + '<(' + arg2_decimal_part[i] + '+' + str(rem) + ') else ' + arg1_decimal_part[i] ) )
res = int( eval( str(upper_digit_to_consider) + '-(' + arg2_decimal_part[i] + '+' + str(rem) + ')' ) )
print("res", str(res))
result = str(res) + result
rem = int( eval( '1 if ' + arg1_decimal_part[i] + '<(' + arg2_decimal_part[i] + '+' + str(rem) + ') else 0' ) )
print("result now:", result)
#data = [2.55 2.55 0.01]
if __name__=='__main__':
r1,r2 = apply_codes_for_coding(2.55, 2.55)
print("r1 r2", r1, r2)
r = overlap(r1, r2, divide_overlap_result_by=1.0)
print("r", r)
r1,r2 = apply_codes_for_coding(r, 2.55)
print("r1 r2", r1, r2)
r = overlap(r1, r2, divide_overlap_result_by=1.0)
print("r", r)
"""r1,r2 = apply_codes_for_coding(r, 1.27)
r = overlap(r1, r2, divide_overlap_result_by=1.0)"""
#------------------------------------
d,d2 = unoverlap(r)
d,d2 = apply_codes_for_decoding(d,d2)
print("d d2", d, d2)
d,d2 = unoverlap(d)
d,d2 = apply_codes_for_decoding(d2,d)
print("d d2", d, d2)
"""print(d)
print(d2)"""
"""d,d2 = unoverlap(d)
d,d2 = apply_codes_for_decoding(d,d2)
print(d)
print(d2)"""
#print( type(sb_addition("-9999999.019999", "-99999.99999993")) )
#print( type(sb_substract("9999999.019999", "99999.99999993")) )
print( type(sb_substract("0.1", "0.9")) )
pass

View file

@ -1,4 +1,13 @@
#!/usr/bin/python3
for i in range(256):
print(i, "==>",i/100)
import numpy
if __name__=='__main__':
pixel_values = numpy.array( [float(i) for i in range(256)] )
print("=================================================================")
print("initial values :", pixel_values)
print("=================================================================")
pixel_values /= 100.0
print("scaled values :", pixel_values)
print("=================================================================")
print("values mean:", pixel_values.mean())