Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Peter's Files
Views: 3893
Visibility: Unlisted (only visible to those who know the link)
Image: ubuntu1804
Kernel: Python 3 (system-wide)

Test Points as Pixels and Shade

import numpy as np import imageio from PIL import Image import math from numba import njit
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-1-468c2947ab96> in <module> 3 from PIL import Image 4 import math ----> 5 from numba import njit ~/.local/lib/python3.6/site-packages/numba/__init__.py in <module> 43 44 # Initialize typed containers ---> 45 import numba.typed 46 47 # Keep this for backward compatibility. ~/.local/lib/python3.6/site-packages/numba/typed/__init__.py in <module> 1 from __future__ import absolute_import 2 ----> 3 from .typeddict import Dict 4 from .typedlist import List ~/.local/lib/python3.6/site-packages/numba/typed/typeddict.py in <module> 16 17 ---> 18 @njit 19 def _make_dict(keyty, valty): 20 return dictobject._as_meminfo(dictobject.new_dict(keyty, valty)) ~/.local/lib/python3.6/site-packages/numba/decorators.py in njit(*args, **kws) 222 warnings.warn('forceobj is set for njit and is ignored', RuntimeWarning) 223 kws.update({'nopython': True}) --> 224 return jit(*args, **kws) 225 226 ~/.local/lib/python3.6/site-packages/numba/decorators.py in jit(signature_or_function, locals, target, cache, pipeline_class, **options) 159 targetoptions=options, **dispatcher_args) 160 if pyfunc is not None: --> 161 return wrapper(pyfunc) 162 else: 163 return wrapper ~/.local/lib/python3.6/site-packages/numba/decorators.py in wrapper(func) 175 disp = dispatcher(py_func=func, locals=locals, 176 targetoptions=targetoptions, --> 177 **dispatcher_args) 178 if cache: 179 disp.enable_caching() ~/.local/lib/python3.6/site-packages/numba/dispatcher.py in __init__(self, py_func, locals, targetoptions, impl_kind, pipeline_class) 574 """ 575 self.typingctx = self.targetdescr.typing_context --> 576 self.targetctx = self.targetdescr.target_context 577 578 pysig = utils.pysignature(py_func) ~/.local/lib/python3.6/site-packages/numba/targets/registry.py in target_context(self) 48 return nested 49 else: ---> 50 return self._toplevel_target_context 51 52 @property ~/.local/lib/python3.6/site-packages/numba/utils.py in __get__(self, instance, type) 379 if instance is None: 380 return self --> 381 res = instance.__dict__[self.name] = self.func(instance) 382 return res 383 ~/.local/lib/python3.6/site-packages/numba/targets/registry.py in _toplevel_target_context(self) 32 def _toplevel_target_context(self): 33 # Lazily-initialized top-level target context, for all threads ---> 34 return cpu.CPUContext(self.typing_context) 35 36 @utils.cached_property ~/.local/lib/python3.6/site-packages/numba/targets/base.py in __init__(self, typing_context) 248 249 # Initialize --> 250 self.init() 251 252 def init(self): ~/.local/lib/python3.6/site-packages/numba/compiler_lock.py in _acquire_compile_lock(*args, **kwargs) 30 def _acquire_compile_lock(*args, **kwargs): 31 with self: ---> 32 return func(*args, **kwargs) 33 return _acquire_compile_lock 34 ~/.local/lib/python3.6/site-packages/numba/targets/cpu.py in init(self) 47 def init(self): 48 self.is32bit = (utils.MACHINE_BITS == 32) ---> 49 self._internal_codegen = codegen.JITCPUCodegen("numba.exec") 50 51 # Add ARM ABI functions from libgcc_s ~/.local/lib/python3.6/site-packages/numba/targets/codegen.py in __init__(self, module_name) 610 self._llvm_module.name = "global_codegen_module" 611 self._rtlinker = RuntimeLinker() --> 612 self._init(self._llvm_module) 613 614 def _init(self, llvm_module): ~/.local/lib/python3.6/site-packages/numba/targets/codegen.py in _init(self, llvm_module) 619 self._tm_features = self._customize_tm_features() 620 self._customize_tm_options(tm_options) --> 621 tm = target.create_target_machine(**tm_options) 622 engine = ll.create_mcjit_compiler(llvm_module, tm) 623 TypeError: create_target_machine() got an unexpected keyword argument 'jitdebug'
class shade(object): def __init__(self, testFunction, bounds=(-1,1,-1,1), dpi=72, backgroundColor=(0,0,0)): self.testFunction = testFunction self.backgroundColor = backgroundColor self.dpi = dpi ## Is there a better way to do the dpi? self.xmin, self.xmax, self.ymin, self.ymax = bounds self.bounds = (self.xmin, self.xmax, self.ymin, self.ymax) self.width = math.ceil((self.xmax-self.xmin) * self.dpi) self.height = math.ceil((self.ymax-self.ymin) * self.dpi) self.pic = Image.new('RGB', (self.width, self.height), self.backgroundColor) self.pixels = self.pic.load() self.h = 0 self.k = 0 def _scale_to_plane(self, h, k): x = self.xmin + h * (self.xmax-self.xmin) / self.width y = self.ymax - k * (self.ymax-self.ymin) / self.height return x, y def fill(self): while self.h < self.width: while self.k < self.height: self.pixels[self.h,self.k] = self.testFunction(*self._scale_to_plane(self.h,self.k)) self.k += 1 self.h += 1 self.k = 0 def save_pic(self, path='Trash.png'): self.pic.save(path)

Simple

def d(a,b): return np.sqrt((a[0]-b[0])**2 + (a[1]-b[1])**2) def simple(x, y): if d((x,y), (0,0)) <= 1: return (255, 255, 255) else: return (0,0,0)
simple = shade(simple) simple.fill() simple.pic
Image in a Jupyter notebook

ALA

def lbda(a,b): return (a+np.sqrt(a**2+2*b))/2 def notToZero(a,b): if (np.linalg.matrix_power(np.array([[a,b],[1/2,0]]),10)>10).any(): return True else: return False def ala(a, b): if notToZero(a,b): return (255, 255, 255) else: return (0,0,0)
ALA = shade(ala, bounds=(0,5,0,5)) ALA.fill() ALA.pic
Image in a Jupyter notebook

Mandelbrot

# @njit def mandelbrotTest(x_0, y_0): x, y = x_0, y_0 # Check for in the first bulb q = (x - 1/4)**2 + y**2 if q * ( q + (x - 1/4)) <= y**2/4: return (0,0,0) # check for 2nd bulb if (x + 1)**2 + y**2 <= 1/16: return (0,0,0) # really outside if x**2 + y**2 >= 4: return (0,0,255) # iterations for i in range(100000): x, y = x**2 - y**2 + x_0, 2 * x * y + y_0 if x**2 + y**2 >= 4: return mandelbrotColor(i + 1) return (0,0,0) # @njit def mandelbrotColor(i): if i == -1: return (0,0,0) red = max(20 * abs((i - 25.5) % 38.25 - 19.125) - 127.5, 0) green = max(20 * abs((i - 12.75) % 38.25 - 19.125) - 127.5, 0) blue = max(20 * abs(i % 38.25 - 19.125) - 127.5, 0) return (int(red), int(green), int(blue))
mand = shade(mandelbrotTest, dpi=72, bounds=(-2.25, 2.25, -2.25, 2.25)) #
mand.fill()
mand.pic
Image in a Jupyter notebook

Weird Lanscape

abs(x**y) < abs(y % x) abs(x % y) < abs(x**y) abs(y**x) < abs(y%x) abs(x**x) < abs(x**y + y**x) and abs(y**y) > abs(x**y + y**x)
def cool(x, y): if y == 0 or x==0: return (255, 0 ,0) if abs(x % y) < abs(x**y) and abs(x**y) < abs(y % x): return (255,255,255) else: return (0,0,0)
new = shade(cool, dpi=100, bounds = (-10,10,-10,10)) new.fill() new.pic

Different

@njit def diffTest(x_0, y_0): x, y = x_0, y_0 # iterations for i in range(100000): x, y = x**2 - y**2, 2 * x * y - .8 if x**2 + y**2 >= 10: return mandelbrotColor(i + 1) return (0,0,0)
diff = shade(diffTest, dpi=75, bounds=(-2.25, 2.25, -2.25, 2.25)) # bounds = (-.25,0,.75,1)) #
diff.fill()
diff.pic
Image in a Jupyter notebook

mand like

@njit def mandLike(x_0, y_0): x, y = x_0, y_0 # iterations for i in range(100000): x, y = x**x_0, y**y_0 if x**2 + y**2 >= 10: return (255,255,255) madLikeColor(i + 1) return (0,0,0) @njit def mandLikeColor(i): if i == -1: return (0,0,0) red = max(20 * abs((i - 25.5) % 38.25 - 19.125) - 127.5, 0) green = max(20 * abs((i - 12.75) % 38.25 - 19.125) - 127.5, 0) blue = max(20 * abs(i % 38.25 - 19.125) - 127.5, 0) return (int(red), int(green), int(blue))
File "<ipython-input-11-532ee8634d66>", line 9 return (255,255,255) madLikeColor(i + 1) ^ SyntaxError: invalid syntax
like = shade(mandLike, dpi=50, bounds = (-10,10,-10,10)) like.fill()
--------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) <ipython-input-12-4c24182b474e> in <module>() 1 like = shade(mandLike, dpi=50, bounds = (-10,10,-10,10)) ----> 2 like.fill() <ipython-input-2-e1cf7f3db49b> in fill(self) 21 while self.h < self.width: 22 while self.k < self.height: ---> 23 self.pixels[self.h,self.k] = self.testFunction(*self._scale_to_plane(self.h,self.k)) 24 self.k += 1 25 self.h += 1 KeyboardInterrupt:
like.pic
Image in a Jupyter notebook