2010-01-15 20 views
11

में मंडेलब्रॉट सेट कार्यान्वयन मैं कई अलग-अलग भाषाओं में Mandelbrot Set के कार्यान्वयन पर काम कर रहा हूं। मेरे पास सी ++, सी #, जावा और पायथन में एक कार्यान्वयन कार्यान्वयन है, लेकिन आम लिस्प कार्यान्वयन में कुछ बग हैं जिन्हें मैं समझ नहीं पा रहा हूं। यह सेट उत्पन्न करता है लेकिन पाइपलाइन में कहीं भी सेट विकृत हो जाता है। मैंने के पास परीक्षण किया है और पता है कि फ़ाइल I/O CLO समस्या नहीं है - यह असंभव है लेकिन संभव है, मैंने इसे बहुत अच्छी तरह से परीक्षण किया है।सामान्य लिस्प

ध्यान दें कि इन कार्यान्वयन का इरादा उन्हें एक दूसरे के खिलाफ बेंचमार्क करना है - इसलिए मैं कोड कार्यान्वयन को यथासंभव समान रखने की कोशिश कर रहा हूं ताकि वे तुलनीय हों।

मैंडलब्रॉट सेट (यहाँ अजगर कार्यान्वयन द्वारा उत्पन्न):

http://www.freeimagehosting.net/uploads/65cb71a873.png http://www.freeimagehosting.net/uploads/65cb71a873.png "मैंडलब्रॉट सेट (अजगर द्वारा उत्पन्न)"

लेकिन मेरे कॉमन लिस्प कार्यक्रम इस उत्पन्न करता है:

http://www.freeimagehosting.net/uploads/50bf29bcc9.png http://www.freeimagehosting.net/uploads/50bf29bcc9.png "कॉमन लिस्प संस्करण का विकृत मंडलब्रॉट सेट "

बग क्लिस और एसबीसीएल दोनों में समान है।

कोड:

कॉमन लिस्प:

(defun mandelbrot (real cplx num_iter) 
    (if (> (+ (* real real) (* cplx cplx)) 4) 
     1 
     (let ((tmpreal real) (tmpcplx cplx) (i 1)) 
     (loop 
      (setq tmpcplx (+ (* (* tmpreal tmpcplx) 2) cplx)) 
      (setq tmpreal (+ (- (* tmpreal tmpreal) (* tmpcplx tmpcplx)) 
       real)) 
      (setq i (+ i 1)) 
      (cond 
       ((> (+ (* tmpreal tmpreal) 
        (* tmpcplx tmpcplx)) 4) (return i)) 
       ((= i num_iter) (return 0))))))) 

(defun floordiv (dend sor) (/ (- dend (mod dend sor)) sor)) 

(defclass xbm() (
    (data :accessor data :initarg :data) 
    (dim :reader dim :initarg :dim) 
    (arrsize :reader arrsize :initarg :arrsize))) 

(defmethod width ((self xbm)) (third (dim self))) 

(defmethod height ((self xbm)) (second (dim self))) 

(defun generate (width height) 
    (let ((dims (list 0 0 0)) (arrsize_tmp 0)) 
     (setq dims (list 0 0 0)) 
     (setf (second dims) height) 
     (setf (third dims) width) 
     (setf (first dims) (floordiv (third dims) 8)) 
     (unless (= (mod width 8) 0) (setf (first dims) (+ (first dims) 1))) 
     (setq arrsize_tmp (* (first dims) (second dims))) 
     (make-instance 'xbm 
     :data (make-array arrsize_tmp :initial-element 0) 
     :dim dims 
     :arrsize arrsize_tmp))) 

(defun writexbm (self f) 
    (with-open-file (stream f :direction :output :if-exists :supersede) 
     (let ((fout stream)) 
     (format fout "#define mandelbrot_width ~d~&" (width self)) 
     (format fout "#define mandelbrot_height ~d~&" (height self)) 
     (format fout "#define mandelbrot_x_hot 1~&") 
     (format fout "#define mandelbrot_y_hot 1~&") 
     (format fout "static char mandelbrot_bits[] = {") 
     (let ((i 0)) 
      (loop 
       (if (= (mod i 8) 0) 
        (format fout "~& ") 
        (format fout " ")) 
       (format fout "0x~x," (svref (data self) i)) 
       (unless (< (setf i (+ i 1)) (arrsize self)) 
        (return t))))))) 

(defmethod setpixel ((self xbm) (x integer) (y integer)) 
    (if (and (< x (third (dim self))) (< y (second (dim self)))) 
     (let ((val (+ (floordiv x 8) (* y (first (dim self)))))) 
     (setf (svref (data self) val) (boole boole-ior (svref (data self) val) (ash 1 (mod x 8))))))) 

(defmethod unsetpixel ((self xbm) (x integer) (y integer)) 
    (if (and (< x (third (dim self))) (< y (second (dim self)))) 
     (let ((val (+ (floordiv x 8) (* y (first (dim self)))))) 
     (setf (svref (data self) val) (boole boole-xor (boole boole-ior 
      (svref (data self) val) (ash 1 (mod x 8))) (ash 1 (mod x 8))))))) 

(defmethod draw_mandelbrot ((xbm xbm) (num_iter integer) (xmin number) 
    (xmax number) (ymin number) (ymax number)) 

    (let ((img_width (width xbm)) (img_height (height xbm)) (xp 0)) 
     (loop 
     (if (< xp img_width) 
      (let ((xcoord (+ (* (/ xp img_width) (- xmax xmin)) xmin)) (yp 0)) 
       (loop 
        (if (< yp img_height) 
        (let (
         (ycoord (+ (* (/ yp img_height) (- ymax ymin)) ymin))) 
         (let ((val (mandelbrot xcoord ycoord num_iter))) 
          (if (> val 0) (unsetpixel xbm xp yp) (setpixel xbm xp yp))) 
         (setq yp (+ yp 1))) 
        (return 0))) 
       (setq xp (+ xp 1))) 
      (return 0))))) 

(defun main() 
    (let ((maxiter 0) (xmin 0) (xmax 0) (ymin 0) (ymax 0) (file nil) (xsize 0) (ysize 0) (picture nil)) 
     (format t "maxiter? ") 
     (setq maxiter (read)) 
     (format t "xmin? ") 
     (setq xmin (read)) 
     (format t "xmax? ") 
     (setq xmax (read)) 
     (format t "ymin? ") 
     (setq ymin (read)) 
     (format t "ymax? ") 
     (setq ymax (read)) 
     (format t "file path: ") 
     (setq file (read-line)) 
     (format t "picture width? ") 
     (setq xsize (read)) 
     (format t "picture height? ") 
     (setq ysize (read)) 
     (format t "~&") 
     (setq picture (generate xsize ysize)) 
     (draw_mandelbrot picture maxiter xmin xmax ymin ymax) 
     (writexbm picture file) 
     (format t "File Written.") 
     0)) 

(main) 

और यह के सबसे करीब है पायथन:

from xbm import * 

def mandelbrot(real_old,cplx_old,i): 
    real = float(real_old) 
    cplx = float(cplx_old) 
    if (real*real+cplx*cplx) > 4: 
     return 1 
    tmpreal = real 
    tmpcplx = cplx 
    for rep in range(1,i): 
     tmpb = tmpcplx 
     tmpcplx = tmpreal*tmpcplx*2 
     tmpreal = tmpreal*tmpreal - tmpb*tmpb 
     tmpcplx += cplx 
     tmpreal += real 
     tmpb = tmpcplx*tmpcplx + tmpreal*tmpreal 
     if tmpb > 4: 
     return rep+1 
    else: 
     return 0 

def draw_mandelbrot(pic, num_iter, xmin, xmax, ymin, ymax): 
    img_width = pic.width() 
    img_height = pic.height() 
    for xp in range(img_width): 
     xcoord = (((float(xp))/img_width) * (xmax - xmin)) + xmin 
     for yp in range(img_height): 
     ycoord = (((float(yp))/img_height) * (ymax - ymin)) + ymin 
     val = mandelbrot(xcoord, ycoord, num_iter) 
     if (val): 
      pic.unsetpixel(xp, yp) 
     else: 
      pic.setpixel(xp, yp) 

def main(): 
    maxiter = int(raw_input("maxiter? ")) 
    xmin = float(raw_input("xmin? ")) 
    xmax = float(raw_input("xmax? ")) 
    ymin = float(raw_input("ymin? ")) 
    ymax = float(raw_input("ymax? ")) 
    file = raw_input("file path: ") 
    xsize = int(raw_input("picture width? ")) 
    ysize = int(raw_input("picture height? ")) 
    print 
    picture = xbm(xsize, ysize) 
    draw_mandelbrot(picture, maxiter, xmin, xmax, ymin, ymax) 
    picture.writexbm(file) 
    print "File Written. " 
    return 0; 

main() 

[xbm.py] 

from array import * 

class xbm: 
    def __init__(self, width, height): 
     self.dim = [0, 0, 0] 
     self.dim[1] = height 
     self.dim[2] = width 
     self.dim[0] = self.dim[2]/8 
     if width % 8 != 0: 
     self.dim[0] += 1 
     self.arrsize = self.dim[0] * self.dim[1] 
     self.data = array('B', (0 for x in range(self.arrsize))) 
     self.hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'] 
    def __nibbletochar__(self, a): 
     if a < 0 or a > 16: 
     return '0' 
     else: 
     return self.hex[a] 
    def setpixel(self, x, y): 
     if x < self.dim[2] and y < self.dim[1]: 
     self.data[(x/8) + (y * self.dim[0])] |= 1 << (x % 8) 
    def unsetpixel(self, x, y): 
     if x < self.dim[2] and y < self.dim[1]: 
     self.data[(x/8) + (y * self.dim[0])] |= 1 << (x % 8) 
     self.data[(x/8) + (y * self.dim[0])] ^= 1 << (x % 8) 
    def width(self): 
     return self.dim[2] 
    def height(self): 
     return self.dim[1] 
    def writexbm(self, f): 
     fout = open(f, 'wt') 
     fout.write("#define mandelbrot_width ") 
     fout.write(str(self.dim[2])) 
     fout.write("\n#define mandelbrot_height ") 
     fout.write(str(self.dim[1])) 
     fout.write("\n#define mandelbrot_x_hot 1") 
     fout.write("\n#define mandelbrot_y_hot 1") 
     fout.write("\nstatic char mandelbrot_bits[] = {") 
     for i in range(self.arrsize): 
     if (i % 8 == 0): fout.write("\n\t") 
     else: fout.write(" ") 
     fout.write("0x") 
     fout.write(self.__nibbletochar__(((self.data[i] >> 4) & 0x0F))) 
     fout.write(self.__nibbletochar__((self.data[i] & 0x0F))) 
     fout.write(",") 
     fout.write("\n};\n") 
     fout.close(); 

मैं C++, C#, या जावा कोड के रूप में अच्छी तरह से पोस्ट कर सकते हैं होना चाहिए

धन्यवाद!

संपादित करें: एडमंड की प्रतिक्रिया के लिए धन्यवाद मुझे बग मिला - बस कुछ जो पोर्टिंग पर दरारों से फिसल गया। संशोधित कोड:

(defun mandelbrot (real cplx num_iter) 
    (if (> (+ (* real real) (* cplx cplx)) 4) 
     1 
     (let ((tmpreal real) (tmpcplx cplx) (i 1) (tmpb cplx)) 
     (loop 
      (setq tmpb tmpcplx) 
      (setq tmpcplx (+ (* (* tmpreal tmpcplx) 2) cplx)) 
      (setq tmpreal (+ (- (* tmpreal tmpreal) (* tmpb tmpb)) 
       real)) 
      (setq i (+ i 1)) 
      (cond 
       ((> (+ (* tmpreal tmpreal) 
        (* tmpcplx tmpcplx)) 4) (return i)) 
       ((= i num_iter) (return 0))))))) 

(defun floordiv (dend sor) (/ (- dend (mod dend sor)) sor)) 

(defclass xbm() (
    (data :accessor data :initarg :data) 
    (dim :reader dim :initarg :dim) 
    (arrsize :reader arrsize :initarg :arrsize))) 

(defun width (self) (third (dim self))) 

(defun height (self) (second (dim self))) 

(defun generate (width height) 
    (let ((dims (list 0 0 0)) (arrsize_tmp 0)) 
     (setq dims (list 0 0 0)) 
     (setf (second dims) height) 
     (setf (third dims) width) 
     (setf (first dims) (floordiv (third dims) 8)) 
     (unless (= (mod width 8) 0) (setf (first dims) (+ (first dims) 1))) 
     (setq arrsize_tmp (* (first dims) (second dims))) 
     (make-instance 'xbm 
     :data (make-array arrsize_tmp :initial-element 0) 
     :dim dims 
     :arrsize arrsize_tmp))) 

(defun writexbm (self f) 
    (with-open-file (stream f :direction :output :if-exists :supersede) 
     (let ((fout stream)) 
     (format fout "#define mandelbrot_width ~d~&" (width self)) 
     (format fout "#define mandelbrot_height ~d~&" (height self)) 
     (format fout "#define mandelbrot_x_hot 1~&") 
     (format fout "#define mandelbrot_y_hot 1~&") 
     (format fout "static char mandelbrot_bits[] = {") 
     (let ((i 0)) 
      (loop 
       (if (= (mod i 8) 0) 
        (format fout "~& ") 
        (format fout " ")) 
       (format fout "0x~x," (svref (data self) i)) 
       (unless (< (setf i (+ i 1)) (arrsize self)) 
        (return t))))))) 

(defun setpixel (self x y) 
    (if (and (< x (third (dim self))) (< y (second (dim self)))) 
     (let ((val (+ (floordiv x 8) (* y (first (dim self)))))) 
     (setf (svref (data self) val) (boole boole-ior (svref (data self) val) (ash 1 (mod x 8))))))) 

(defun unsetpixel (self x y) 
    (if (and (< x (third (dim self))) (< y (second (dim self)))) 
     (let ((val (+ (floordiv x 8) (* y (first (dim self)))))) 
     (setf (svref (data self) val) (boole boole-xor (boole boole-ior 
      (svref (data self) val) (ash 1 (mod x 8))) (ash 1 (mod x 8))))))) 

(defun draw_mandelbrot (xbm num_iter xmin xmax ymin ymax) 

    (let ((img_width (width xbm)) (img_height (height xbm)) (xp 0)) 
     (loop 
     (if (< xp img_width) 
      (let ((xcoord (+ (* (/ xp img_width) (- xmax xmin)) xmin)) (yp 0)) 
       (loop 
        (if (< yp img_height) 
        (let (
         (ycoord (+ (* (/ yp img_height) (- ymax ymin)) ymin))) 
         (let ((val (mandelbrot xcoord ycoord num_iter))) 
          (if (> val 0) (unsetpixel xbm xp yp) (setpixel xbm xp yp))) 
         (setq yp (+ yp 1))) 
        (return 0))) 
       (setq xp (+ xp 1))) 
      (return 0))))) 

(defun main() 
    (let ((maxiter 0) (xmin 0) (xmax 0) (ymin 0) (ymax 0) (file nil) (xsize 0) (ysize 0) (picture nil)) 
     (format t "maxiter? ") 
     (setq maxiter (read)) 
     (format t "xmin? ") 
     (setq xmin (read)) 
     (format t "xmax? ") 
     (setq xmax (read)) 
     (format t "ymin? ") 
     (setq ymin (read)) 
     (format t "ymax? ") 
     (setq ymax (read)) 
     (format t "file path: ") 
     (setq file (read-line)) 
     (format t "picture width? ") 
     (setq xsize (read)) 
     (format t "picture height? ") 
     (setq ysize (read)) 
     (format t "~&") 
     (setq picture (generate xsize ysize)) 
     (draw_mandelbrot picture maxiter xmin xmax ymin ymax) 
     (writexbm picture file) 
     (format t "File Written.") 
     0)) 

(main) 

हालांकि कोड बहुत लिस्प-ish नहीं है (एक शब्द है?) यह काम करता है। सब जो पोस्ट/टिप्पणी की लिए धन्यवाद/जवाब :)

+0

आप छवियों या एकाधिक यूआरएल पोस्ट नहीं कर सकते क्योंकि आप एक नए सदस्य हैं। यह नियम केवल स्पैम पर कटौती करना है। मैंने आपके पोस्ट को – Jimmy

+0

संपादित किया है क्या आप जानते हैं कि xbm फ़ाइल पीढ़ी lisp संस्करण में सही है? शायद इसके लिए कुछ यूनिट परीक्षण उचित होंगे (यानी एक वर्ग और एक सर्कल खींचे और देखें कि क्या वे सही बाहर आते हैं)। –

+1

क्या होता है यदि आप अपनी कुछ संख्याओं को डबल-फ्लोट करते हैं? यह भी ध्यान रखें कि जटिल संख्या सीएल का हिस्सा हैं। – Brian

उत्तर

5

मुझे यकीन है कि इस हिस्से सही है नहीं कर रहा हूँ:

 (setq tmpcplx (+ (* (* tmpreal tmpcplx) 2) cplx)) 
     (setq tmpreal (+ (- (* tmpreal tmpreal) (* tmpcplx tmpcplx)) 
      real)) 

Isn ' टी tempcplx पहली पंक्ति पर अपने नए मूल्य के साथ अधिलेखित किया जा रहा है, जिसका अर्थ है कि दूसरी पंक्ति नए मूल्य का उपयोग कर रहा है, मूल नहीं?

अजगर संस्करण आप tmpb का उपयोग करके इस समस्या को अनदेखा कर रहे हैं:

tmpb = tmpcplx 
    tmpcplx = tmpreal*tmpcplx*2 
    tmpreal = tmpreal*tmpreal - tmpb*tmpb 
    tmpcplx += cplx 
    tmpreal += real 

मुझे ऐसा लगता है लिस्प संस्करण, कुछ इसी तरह करते हैं यानी पहले tmpcplx के मूल मूल्य की दुकान, और उस दुकान का उपयोग करना चाहिए tmpreal की गणना के लिए:

 (setq tmpb cplx) 
     (setq tmpcplx (+ (* (* tmpreal tmpcplx) 2) cplx)) 
     (setq tmpreal (+ (- (* tmpreal tmpreal) (* tmpb tmpb)) 
      real)) 
+3

इस तरह के समांतर असाइनमेंट के लिए आम लिस्प में पीएसईटीएफ है। – Svante

10

अपने कोड के बारे में कुछ टिप्पणी:

  • मैंडलब्रॉट: घोषणाओं का अभाव है, चौराहों

  • मैंडलब्रॉट पाश में दो बार गणना: गणना में TMPREAL के लिए आप टीएमपीसीएलएक्स के नए मूल्य का उपयोग कर रहे हैं, पुराना एक

  • आप पिक्सल सेट करने के लिए METHODS का उपयोग नहीं करना चाहते हैं। धीमा।

  • FLOORDIV मंजिल या TRUNCATE में से एक कॉमन लिस्प में (आप क्या चाहते हैं पर निर्भर करता है), को देखने के (मंजिल 10 3)

  • उपयोग प्रकार घोषणाओं

  • writexbm में

    बार बार डेटा कॉल नहीं करते है और ARRSIZE

  • setpixel, unsetpixel बहुत महंगा, फिर बार-बार संरचना

  • ड्रॉ-मैंडलब्रॉट अपसंदर्भन लग रहा है आर का एक बहुत है epeated संगणना है कि एक बार किया जा सकता है

  • कॉमन लिस्प 2 डी सरणियों जो कोड

  • कॉमन लिस्प जटिल संख्या है, जो भी कोड को आसान बनाने में है को आसान बनाने में है

  • एक चर नाम 'स्वयं' नहीं बनाता है सामान्य लिस्प में भावना। इसे नाम दें कि यह क्या है।

आम तौर पर कोड अपशिष्ट से भरा होता है। यह आपके कोड को बेंचमार्क करने के लिए बहुत कम समझ में आता है, क्योंकि यह एक ऐसी शैली में लिखा गया है जो उम्मीद है कि कोई भी आम लिस्प में उपयोग नहीं करता है। सामान्य लिस्प को मैक्सिमा जैसे बड़े गणितीय सॉफ़्टवेयर के अनुभव के साथ डिजाइन किया गया है और गणितीय कोड को सीधे आगे बढ़ने की अनुमति देता है (कोई ऑब्जेक्ट नहीं, केवल संख्याओं, सरणी, ... पर कार्य करता है)। बेहतर कंपाइलर आदिम प्रकार, आदिम परिचालन और प्रकार की घोषणाओं का लाभ उठा सकते हैं। इस प्रकार स्टाइल अलग है जो पाइथन में लिख सकता है (जो आम तौर पर ऑब्जेक्ट-ओरिएंटेड पायथन या कुछ सी कोड पर कॉल करता है) या रूबी। भारी संख्यात्मक कोड में आमतौर पर CLOS के साथ गतिशील प्रेषण होना अच्छा विचार नहीं है। सीएलओएस कॉल के माध्यम से बिटमैप्स में पिक्सेल सेट करना एक कसौटी लूप में कॉल करता है वास्तव में कुछ ऐसा टालना चाहता है (जब तक कि आप इसे अनुकूलित करने के बारे में नहीं जानते)।

बेहतर लिस्प कंपाइलर संख्यात्मक कार्यों को सीधे मशीन कोड में संकलित करेंगे। संकलन के दौरान वे संकेत देते हैं कि संचालन सामान्य हैं और अनुकूलित नहीं किए जा सकते हैं (जब तक डेवलपर अधिक प्रकार की जानकारी नहीं जोड़ता)। डेवलपर कार्यों को 'अक्षम' भी कर सकता है और जेनेरिक या अनावश्यक फ़ंक्शन कॉल करने वाले कोड की जांच कर सकता है। '। के न्यूमेरिक कोड consing में'। "समय तो 'क्रम जानकारी देता है और यह भी स्मृति की मात्रा के बारे में डेवलपर को सूचित' consed तैरता 'है एक सामान्य प्रदर्शन समस्या

, योग करने के लिए:

  • यदि आप कोड लिखते हैं और सोचते हैं कि यह अलग-अलग भाषाओं में समान होता है, जब कोड समान दिखता है या एक समान संरचना है, तो यह मामला नहीं हो सकता है - जब तक कि आप वास्तव में दोनों भाषाओं और दोनों भाषा कार्यान्वयन को नहीं जानते।

  • यदि आप एक भाषा में कोड लिखते हैं और इसे एक समान शैली में एक अलग भाषा में पोर्ट करते हैं, तो आप एक wh को याद कर सकते हैं इस तरह की समस्याओं के समाधान को अलग तरीके से लिखने के लिए मौजूदा संस्कृति को ओले करें। उदाहरण के लिए कोई ऑब्जेक्ट उन्मुख शैली में सी ++ में कोड लिख सकता है और इसे फोरट्रान के समान तरीके से पोर्ट कर सकता है। लेकिन कोई भी फोरट्रान में ऐसा कोड नहीं लिखता है। फोरट्रान शैली में लिखे गए, आमतौर पर तेज़ कोड में परिणाम होंगे - खासकर जब कंप्यूटर्स को इडियोमैटिक फोरट्रान कोड के लिए अत्यधिक अनुकूलित किया जाता है।

  • "जब रोम में, रोमनों की तरह बात"

उदाहरण:

SETPIXEL में वहाँ के लिए एक कॉल है (पहले (मंद स्वयं))। इस सूची को पहली बार सूची में एक स्लॉट क्यों नहीं बनाते हैं, हर समय सूची एक्सेस करने के बजाय? लेकिन फिर गणना गणना के दौरान स्थिर है। फिर भी संरचना पारित की जाती है, और मूल्य हर समय पुनर्प्राप्त किया जाता है। क्यों न केवल मुख्य पाश के बाहर मूल्य प्राप्त करें और इसे सीधे पास करें? इसके कई कंप्यूटेशंस करने के बजाय?

आपको यह विचार देने के लिए कि कोड कैसे लिखा जा सकता है (प्रकार की घोषणाओं, लूप, जटिल संख्याओं के साथ ...), यहां मंडलब्रॉट गणना का थोड़ा अलग संस्करण है।

कोर एल्गोरिथ्म:

(defvar *num-x-cells* 1024) 
(defvar *num-y-cells* 1024) 
(defvar *depth* 60) 


(defun m (&key (left -1.5) (top -1.0) (right 0.5) (bottom 1.0) (depth *depth*)) 
    (declare (optimize (speed 3) (safety 0) (debug 0) (space 0))) 
    (loop with delta-x-cell float = (/ (- right left) *num-x-cells*) 
     and delta-y-cell float = (/ (- bottom top) *num-y-cells*) 
     and field = (make-array (list *num-x-cells* *num-y-cells*)) 
     for ix fixnum below *num-x-cells* 
     for x float = (+ (* (float ix) delta-x-cell) left) 
     do (loop for iy fixnum below *num-y-cells* 
       for y = (+ (* (float iy) delta-y-cell) top) 
       do (loop for i fixnum below depth 
          for z of-type complex = (complex x y) 
          then (+ (complex x y) (* z z)) 
          for exit = (> (+ (* (realpart z) (realpart z)) 
              (* (imagpart z) (imagpart z))) 
             4) 
          finally (setf (aref field ix iy) i) 
          until exit)) 
     finally (return field))) 

समारोह से ऊपर नंबर की एक 2 डी सरणी देता है।

एक XBM फ़ाइल लिखी:

(defun writexbm (array pathname &key (black *depth*)) 
    (declare (fixnum black) 
      (optimize (speed 3) (safety 2) (debug 0) (space 0))) 
    (with-open-file (stream pathname :direction :output :if-exists :supersede) 
    (format stream "#define mandelbrot_width ~d~&" (array-dimension array 0)) 
    (format stream "#define mandelbrot_height ~d~&" (array-dimension array 1)) 
    (format stream "#define mandelbrot_x_hot 1~&") 
    (format stream "#define mandelbrot_y_hot 1~&") 
    (format stream "static char mandelbrot_bits[] = {") 
    (loop for j fixnum below (array-dimension array 1) do 
      (loop for i fixnum below (truncate (array-dimension array 0) 8) 
       for m fixnum = 0 then (mod (1+ m) 8) do 
       (when (zerop m) (terpri stream)) 
       (format stream "0x~2,'0x, " 
         (let ((v 0)) 
          (declare (fixnum v)) 
          (dotimes (k 8 v) 
          (declare (fixnum k)) 
          (setf v (logxor (ash (if (= (aref array 
                   (+ (* i 8) k) j) 
                 black) 
                1 0) 
               k) 
              v))))))) 
    (format stream "~&}~&"))) 

समारोह से ऊपर एक सरणी और एक पथ नाम लेता है और XBM फ़ाइल के रूप में देता है लिखता है। एक नंबर 'काली' 'काली' हो जाएगा और अन्य नंबरों 'सफेद' हैं

कॉल

(writexbm (m) "/tmp/m.xbm") 
+2

का एक हिस्सा हैं, मैं पूरी तरह से "जब रोम में" तर्क समझता हूं, लेकिन मैंने पहले फैसला किया था कि मुझे एक शैली में लिखना होगा कि मैं भाषाओं में लगातार रह सकता हूं क्योंकि अगर मैंने एक शैली में लिखा है सी ++ बनाम अन्य भाषाओं के साथ मेरी प्रवीणता के कारण किसी भी परिणाम को अवैध कर दिया जाएगा। अगर मैं सिर्फ वैक्यूम में कोड लिख रहा था तो मैं निश्चित रूप से आपके कोड का उपयोग करता हूं। लेकिन मूल कोड (मेरे पिछड़े तर्क के साथ) दिमाग में अनुकूलन (मेरे अपने पूर्वाग्रहों से परे) के साथ लिखा नहीं था। आप स्पष्ट रूप से लिस्प में अधिक कुशल हैं, और मैं आपके कोड का अध्ययन करने की कोशिश करूंगा जब तक कि मैं इसे समझ नहीं पाता। धन्यवाद! –

+3

अब परिणाम अमान्य हैं, क्योंकि आप सोचते हैं कि आपका कोड समान है या ऐसा कुछ समान है, लेकिन वास्तव में ऐसा नहीं है। उदाहरण में ए + बी सी और (+ ए बी) लिस्प में समान प्रतीत होता है, लेकिन वे नहीं हैं। लिस्प डिफ़ॉल्ट रूप से फिक्स्डम्स, बिग्नम, कॉम्प्लेक्स, अनुपात, फ्लोट्स के साथ एक सामान्य संख्यात्मक है ... यह सी कोड से बहुत अलग व्यवहार करता है, हालांकि बयान समान दिखते हैं। –

+0

मैं कैसे 5 स्टाइलिस्टिक रूप से भिन्न प्रोग्रामिंग भाषाओं में कोड निष्पादित करता हूं? प्रत्येक भाषा में इसकी मूर्खताएं होती हैं, और ए + बी और (+ बी) जानबूझकर भिन्न होती हैं। सभी भाषाओं को बराबर नहीं बनाया गया है। डिजाइन प्रक्रिया में निर्णय एक भाषा की दक्षता को बदलते हैं। गतिशील प्रकारों के साथ रनटाइम वातावरण होने से गति प्रभावित होगी, और वह भाषा का एक डिज़ाइन निर्णय था। मेरा इरादा भाषा के चारों ओर काम नहीं करना है बल्कि काम के चारों ओर भाषा काम करना है। हां, यह एक प्रक्रियात्मक मानसिकता है- लेकिन मैं शैलियों को बदल नहीं सकता और पारस्परिकता सुनिश्चित नहीं कर सकता। –

संबंधित मुद्दे