Author Topic: A question for Charles, John , Mike & others.  (Read 2297 times)

0 Members and 1 Guest are viewing this topic.

RobbeK

  • Sr. Member
  • ****
  • Posts: 368
A question for Charles, John , Mike & others.
« on: May 09, 2015, 10:38:41 AM »
It's about basic , more specific Freebasic.

Wrote an interface for the NewtonRaphson - very general , but now it has to take 12 complex powers to start ...
Works great , but slow.
The reason is that it calculates something as

a*z^7 + b*z^6 + ....   a0  ... of course in many cases the constants (like pe "b") are 0 (see code) and there is no need in calculating the power, the result will be 0 anyway .. is there an elegant way besides nesting/building if's to do this ..
in Lisp (because it's functional) one can say    (+   ...  (when (not (zero? a)) (* a (expt z 7)))  ....  (when (not (zero? b .... etc  ..

Is the only way something as

if a <> 0 then result = result + a*calculation
if b <> 0 then result = resiult + b*     .....   etc

or can one write macro's for this ??????  ... or even conditional compiling ??

thanks in advance
Rob





.
« Last Edit: May 09, 2015, 11:03:22 AM by RobbeK »

jack

  • Jr. Member
  • **
  • Posts: 90
Re: A question for Charles, John , Mike & others.
« Reply #1 on: May 09, 2015, 11:43:23 AM »
hello RobbeK
I am going to try compiling with different compiler options and see what produces the fastest exe.
btw, you may want to give the latest version of FB a try, with the latest version, compiling using -gen gcc -Wc O2 may significantly increase performance.
« Last Edit: May 09, 2015, 11:51:00 AM by jack »

jack

  • Jr. Member
  • **
  • Posts: 90
Re: A question for Charles, John , Mike & others.
« Reply #2 on: May 09, 2015, 12:50:50 PM »
no luck getting a faster exe using gen gcc with FB32 but the 64-bit version is about 60% faster

.

RobbeK

  • Sr. Member
  • ****
  • Posts: 368
Re: A question for Charles, John , Mike & others.
« Reply #3 on: May 09, 2015, 01:23:14 PM »
Hi , Thanks Jack ...

I have to find a way to remove those 0*z^something out of calculations  -- in Lisp it's dead easy to generate such data and eval them ..  I'll try some conditionals and see where this ends .. 


best, Rob

Mike Lobanovsky

  • Admin Support Member
  • *****
  • Posts: 1854
Re: A question for Charles, John , Mike & others.
« Reply #4 on: May 09, 2015, 01:32:13 PM »
Rob,

You aren't fair. Your when not zero a ... when not zero b ... when not zero c ... are a crooked functional Lispish way ( :P ) to say something that's quite naturally and elegantly expressed in a ton of other languages including FreeBASIC as

if a then result += calculation
if b then result += calculation

etc.

 :P at you many times again!

(Let Charles respond with how C-stylish #defines and #macros are used in FB. He must be a guru in these matters. :) )
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, 2 x nVidia GTX 650Ti w/ 2GB VRAM, Windows 7 Ultimate Sp1)

jack

  • Jr. Member
  • **
  • Posts: 90
Re: A question for Charles, John , Mike & others.
« Reply #5 on: May 09, 2015, 02:08:00 PM »
RobbeK
I suggest you write an integer exponentiation function and use that instead of the power operator, it should be much faster

RobbeK

  • Sr. Member
  • ****
  • Posts: 368
Re: A question for Charles, John , Mike & others.
« Reply #6 on: May 09, 2015, 02:18:28 PM »
Hi Mike,

  :)
well, yes in principle Lisp has no statements , the "when's and if's" return a value anyway so you can use them inside calculations.  But the imperative  way works as well, the return object is just neglected.

(when (not (zero? a)) (set! result  ;...    etc ;..     )

however,
the lispish way is to build a list with all the data containing a zero multiplication removed , & then  this data evaluated as code.
This needs only 14 conditionals to build / image -- the other way (without macro's and a preprocessing) and keeping them inside the loops needs 64000*14*(# of iterations)  conditionals to test.  I did not make a compare with basic,  -- because I only know GFA basic that can do such things (i hope being wrong here )

best Rob


RobbeK

  • Sr. Member
  • ****
  • Posts: 368
Re: A question for Charles, John , Mike & others.
« Reply #7 on: May 09, 2015, 02:47:20 PM »
Hi Jack,

The manual mentions :

 Z complex, a integer : repeated multiplications with Legendre’s al-
gorithm to minimize the number of operations. For instance, Z 8 is
computed as Z 2 = Z · Z, Z 4 = Z 2 · Z 2 and Z 8 = Z 4 · Z 4 , hence 3
multiplications instead of 7.

• Z complex, a real : DeMoivre’s theorem :
Z a = [Rexp(iθ)] a = R a exp(aiθ) = R a (cosaθ + isinaθ)

• Z real or complex, a complex : Z a = exp(alnZ)

so, it should in this case automatically use the Lengendre algorithm

I'll write (based on FBcomplex) a DLL that I can use in PLT Scheme ,  and code some optimizations ..

best, Rob

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3719
    • Oxygen Basic
Re: A question for Charles, John , Mike & others.
« Reply #8 on: May 09, 2015, 03:13:02 PM »
FreeBasic offers C style #define, and its own multi-line #macro(..) ... #endmacro

Code: [Select]
    #macro adrf(afun)
      call afun
      call addrunfun
    #endmacro


RobbeK

  • Sr. Member
  • ****
  • Posts: 368
Re: A question for Charles, John , Mike & others.
« Reply #9 on: May 09, 2015, 03:25:37 PM »
Hi Charles,

I'll read the documentation , can it use #if , #then inside the macro / #define ? 

Rob


Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3719
    • Oxygen Basic
Re: A question for Charles, John , Mike & others.
« Reply #10 on: May 09, 2015, 03:48:54 PM »
I would say yes, but there are ambiguities, so it is best to run a few tests to see the order of expansion with #macro and #if, especially when involving macro params.

Code: [Select]
#if (expression)

' Conditionally included statements

#endif

RobbeK

  • Sr. Member
  • ****
  • Posts: 368
Re: A question for Charles, John , Mike & others.
« Reply #11 on: May 10, 2015, 01:56:00 AM »
Thanks Charles ,

Looking for something similar  as :    (lisp)

(define a 7)
(define b 6)
(define x 2)
(define y -6)
(define z 1)   ;; just as an example

(define form '((* 5 x)  (* 7 y) (* 0 z) (* 22 a) (* 0 b)))  ;; just an example

(define (use-addition-on formula)
  (eval (cons '+ formula)))                                ;; turns data into code

(define (simplify* L M)
  (if (null? L) M
     (if (zero? (cadar L)) (simplify* (cdr L) M)
                         (simplify* (cdr L) (cons (car L) M)))))     ;; the glorious simplificator , processing the data

(define (simplify L) (simplify* L null))                            ;; simplifying the simplificator !!


--------------------------------------------------------------------REPL

Language: scheme [custom]; memory limit: 128 MB.
> form
((* 5 x) (* 7 y) (* 0 z) (* 22 a) (* 0 b))


> (simplify form)
((* 22 a) (* 7 y) (* 5 x))                                   ;; the purified list
> (use-addition-on form)
122

> (use-addition-on (simplify form))
122

> (define simplified-form (simplify form))                                    ;; as a preprocessor  only has to be done once
> (use-addition-on simplified-form)

122
>

that's power !