def generate_fixnum_a_less_than_b(): a = random_python_int_inside_fixnum_range() b = random_python_int_inside_fixnum_range() while a == b: b = random_python_int_inside_fixnum_range() if a < b: return Numeric(str(a)), Numeric(str(b)) else: return Numeric(str(b)), Numeric(str(a))
def random_exact_complexnum_and_equiv_python_exactcomplex(): real_numerator = random_python_int_outside_fixnum_range() real_denominator = random_python_int_outside_fixnum_range() imaginary_numerator = random_python_int_outside_fixnum_range() imaginary_denominator = random_python_int_outside_fixnum_range() return Numeric(str(real_numerator), str(real_denominator), str(imaginary_numerator), str(imaginary_denominator)), ExactComplex( Fraction(real_numerator, real_denominator), Fraction(imaginary_numerator, imaginary_denominator))
def _prepareFGAndInitInput(self, contentImgname, styleImgname, init='mixed'): print 'prepare img_content...' img_content = scipy.misc.imread(contentImgname) self.ori_width = img_content.shape[0] self.ori_height = img_content.shape[1] self.img_content = img_content # resize it to square width = self.net.blobs['data'].shape[2] img_content = scipy.misc.imresize(img_content, (width, width)) # transform it x = self.transformer.preprocess('data', img_content) self.F_content = Numeric().computeFandG(x, self.net, [], self.content_layers)[0] print 'prepare style_content...' img_style = scipy.misc.imread(styleImgname) self.img_style = img_style img_style = scipy.misc.imresize(img_style, (width, width)) # transform it x = self.transformer.preprocess('data', img_style) self.G_style = Numeric().computeFandG(x, self.net, self.style_layers, [])[1] print 'prepare init input...' if init == 'mixed': img0 = 0.95 * self.transformer.preprocess('data', img_content) +\ 0.05 * self.transformer.preprocess('data', img_style) else: img0 = self.transformer.preprocess('data', img_content) # compute data bounds data_min = -self.transformer.mean["data"][:, 0, 0] data_max = data_min + 255 data_bounds = [(data_min[0], data_max[0])] * (img0.size / 3) + \ [(data_min[1], data_max[1])] * (img0.size / 3) + \ [(data_min[2], data_max[2])] * (img0.size / 3) self.img0 = img0 self.data_bounds = data_bounds
def transfer(self): """ Do the transfer job :return: """ self.tool = ST(self.model_dirs, self.content_img, self.style_img, self.device_id) self.optimizer = Numeric() # construct the optimize param method = 'L-BFGS-B' args = (self.tool.getNet(), self.tool.getLayers(), self.tool.getFcontent(), self.tool.getGstyle(), self.tool.getStyleLayer(), self.tool.getContentLayer(), self.tool.getRatio()) jac = True bounds = self.tool.getBounds() img0 = self.tool.getImg0() options = {'maxiter': self.max_iter, 'disp': self.verbose} res = minimize(self.optimizer.computeLossAndGradAll, img0.flatten(), args=args, method=method, jac=jac, bounds=bounds, options=options) data = self.tool.net.blobs['data'].data[0,...] self.tool.saveImg(data)
def random_inexact_complexnum_and_equiv_python_floats( ) -> Tuple[Numeric, float, float]: real = random_float() imaginary = random_float() return Numeric(real, imaginary), real, imaginary
def random_inexact_complexnum() -> Numeric: return Numeric(random_float(), random_float())
def random_fixnum() -> Numeric: return Numeric(str(random_python_int_inside_fixnum_range()))
def random_ratnum_and_equiv_python_fraction(): numerator = random_python_int_outside_fixnum_range() denominator = random_python_int_outside_fixnum_range() return Numeric(str(numerator), str(denominator)), Fraction(numerator, denominator)
def random_fixnum_and_equiv_python_int() -> Tuple[Numeric, int]: py_int = random_python_int_inside_fixnum_range() return Numeric(str(py_int)), py_int
def random_exact_complexnum(): return Numeric(str(random_python_int_outside_fixnum_range()), str(random_python_int_outside_fixnum_range()), str(random_python_int_outside_fixnum_range()), str(random_python_int_outside_fixnum_range()))
from random import randint, uniform, seed from functools import partial from typing import Tuple from fractions import Fraction from test_suites.util_types import ExactComplex MIN_FIXNUM_VALUE = -4611686018427387904 MAX_FIXNUM_VALUE = 4611686018427387903 MAX_ARBITRARY_NUM_BITS = 256 seed(9439832) random_float = partial(uniform, -1e12, 1e12) zero = Numeric("0") one = Numeric("1") negative_one = Numeric("-1") def generate_fixnum_a_less_than_b(): a = random_python_int_inside_fixnum_range() b = random_python_int_inside_fixnum_range() while a == b: b = random_python_int_inside_fixnum_range() if a < b: return Numeric(str(a)), Numeric(str(b)) else: return Numeric(str(b)), Numeric(str(a))
def runWithVoice(self, rulefile): def clearScreen(): os.system('clear') def str2int(content): return int(content) self.outputLine('请说出您的名字吧'); name = self.speech.getVoiceText() if name is not None: names = self.nlp.getPersons(name) if names is not None: name = names[0] if name is None: name = '' self.outputLine('欢迎您!{}'.format(name)); formulaRule = FormulaRule(rulefile) self.outputLine('总共是{}关,您想从第几关开始?'.format(formulaRule.getSize())) text = self.speech.getVoiceText() if text is not None: text = Numeric.chinese2arabic(text) numbers = getMatchList(r'\d+', text, str2int) if len(numbers) > 0: formulaRule.set(numbers[0] - 1) while True: rule = formulaRule.createFormula() clearScreen() self.outputLine(u'当前第{}关,如果需要跳过或者退出,请说出跳过或者退出。'.format(formulaRule.getIndex() + 1)) text = self.speech.getVoiceText() if text is not None and self.nlp.isSimilar('退出', text): self.outputLine('已退出') break if text is None or not self.nlp.isSimilar('跳过', text): clearScreen() formula = Formula.parse(rule) self.outputLine(u'第{}关题目如下:'.format(formulaRule.getIndex() + 1)) msg = '{} = 多少?'.format(formula) for i in range(3): if i is not 0: self.outputLine('请再想想吧?') self.outputLine(msg) text = self.speech.getVoiceText() if text is None: continue text = Numeric.chinese2arabic(text) items = self.nlp.getNumbers(text) if items is None: continue result = Currency.createFromReadableString(items) if result is None: continue answer = result.toCurrency() correct = False try: condition = '{0} == {1} or ({0} > {1} and {0} < {1} + 0.01) or ({1} > {0} and {1} < {0} + 0.01)'.format(rule, answer) if eval(condition): correct = True except Exception as e: pass try: answer = Formula.parse(answer) answer = answer.getResult() except CurrencyException as e: pass self.outputLine('{} = {}'.format(formula, answer)) if correct: self.outputLine('回答正确!恭喜您通过第{}关!'.format(formulaRule.getIndex() + 1)) break else: self.outputLine('可能您不太理解,正确答案是:{} = {}。'.format(formula, formula.getResult())) continue else: self.outputLine(u'已跳过第{}关。'.format(formulaRule.getIndex() + 1)) clearScreen() if not formulaRule.forward(): self.outputLine('恭喜您!{},通过全部{}关!'.format(name, formulaRule.getSize())) break