Welcome to Fractal Forums

Fractal Software => Programming => Topic started by: hsmyers on August 05, 2013, 11:08:22 PM




Title: Ultra Fractal Class question
Post by: hsmyers on August 05, 2013, 11:08:22 PM
I'm writing a utility class for a group of formulas with similar needs. One 'need' is to return a selected float parameter from an array of values. At the moment the only way I've been able to do this is to embed the array in the utility function itself. At guess there must be a better way? Ideally I'd like the equivalent of static const or even what I might get from declaring it in the global: section of the formula. Can this be done? Not knowing just how UF instantiates the class I suppose there is a chance that this is not needed, but I don't know. BTW whenever I attempt to move it out of the function I get a parser error about expecting a field---what's up with that?

In sum I'd like:

  • declaration exterior to the utility function
  • instantiated once, not repeatedly

Actually I suppose I'd like a real manual about class coding for UF, but I'll take any help I can get :)

--hsm


Title: Re: Ultra Fractal Class question
Post by: cKleinhuis on August 05, 2013, 11:22:01 PM
so, can you be more specific about your array !?
is it different for different needs, is it created "on the fly"?
depending on what you do, various approaches might fit,
i am not perfect at coding uf, bot most of the stuff works somehow,
and the class system in general is just perfect for different formulas
and recombining them

so, if all classes shall have access to the same array, hmm, a memory
consumption approach would be to instanciate a class defining the array
in each of the classes ...

one approach to propagate the data to sub-property-classes might be
to define a base class for distributing an array to work with, and
the accessing functions for the array in the same base class
so that you could choose the array in a "root" formula, and in the
init part of your function you propagate the data to the lower classes
something like this:

in a base function
var theArrayClass
setData(arrayclass)
getDataFromArray(anydimensionalindex)

and then in the root class you let the user define the
array to work with, and in the init class you
call the setData function, which then propagates
the array to sub classes

clearly, any class using arrays should define the sub-classes
in an array like manner as well for accessing possible sub classes
to propagate the array to :(



Title: Re: Ultra Fractal Class question
Post by: hsmyers on August 06, 2013, 12:25:00 AM
Here is a shortened version of what works, not what I'd like  :angry::

Code:
Class Util {

  static float func GetConstant(int e)
    ;
    ; Mathematical constants
    ;
    float fConstant[5] ;real version is size of 25
    ; Pi
    fConstant[0] = 3.1415926535897932384626433832795028841971693993751
    ; Degree
    fConstant[1] = 0.017453292519943295769236907684886127134428718885417
    ; Base of the natural logarithm
    fConstant[2] = 2.7182818284590452353602874713526624977572470937
    ; Golden ratio
    fConstant[3] = 1.6180339887498948482045868343656381177203091798058
    ; Euler’s Constant
    fConstant[4] = 0.57721566490153286060651209008240243104215933593992

    return fConstant[e]
  endfunc
}

Calling is essentially:
Code:
  float A = Util.GetConstant(@enumparam_A)
  float B = Util.GetConstant(@enumparam_B)
BTW, the implication from the manual is that static functions shouldn't need the 'Util.' portions, yet that errors out. I'm guessing that what was meant was that I don't have instantiate the class with 'new'?

I am not yet ready to do a tiered class system for the formulas---I'm still learning UF5 class code so I'm hoping to ease into things.

--hsm


Title: Re: Ultra Fractal Class question
Post by: David Makin on August 06, 2013, 05:29:08 PM
First of all note that pi is accessed as #pi (to working resolution even if arbitrary) and so is exp(1) as #e - also using #pi/360.0 or similar in the code will pre-calculate at compile time.

As for writing formulas/colourings etc:

Rule 1: If you don't really need class-based formulas *use the old style* - class-based formulas are more generic and therefore *considerably* slower to execute because of the overheads required.

In the old style to do what you require simply add:

global:
  static float f[5]
  f[0]=1
  f[1]=2
  f[2]=3
  f[3]=4
  f[4]=5 ; or whatever constants are required

Before the init: section and in the code simply return a constant index i as f[ i ]

If you wish to use this in class-based formulas/colourings then the best way is have your own derived main formula base class and/or derived colouring formula base classes for divergent/convergent/both - simply put the code from the global section above in your derived base's init section - but with "static float f[5]" in the base's global variables section (protected/public etc.as you wish).


Title: Re: Ultra Fractal Class question
Post by: hsmyers on August 06, 2013, 11:14:50 PM
Much thanks! I'll give it a try.

Regarding built ins, yes I'm aware of that. That said this is a port of software written in the 80's and I'm pretty sure the other 23 constants are not conveniently lying around  :) I've been at this since the Scientific American article and started writing fractal formulas and software at the same time as the stone soup gang, so there is a slim chance I'm not as un-aware as you might think. I'm new to UF5, not fractals. Thanks again!

--hsm
p.s. I could almost swear that I tried global: but perhaps not with static?
p.s.s. Clearly I don't understand as my translation of your suggestion:

Code:
Class Util {

init: ;errors out at this line

global:

    ;
    ; Mathematical constants
    ;
    static float fConstant[25]

doesn't work either  :sad1:
p.s.s.s. Use larger hammer, I'm thick headed!