in reply to A data selection problem(in3D).

When doing a two-dimensional "s" shaped (sigmoid) curve, I often use a polynomial sigmoid*,**: f(x) = 3*x**2 - 2*x**3, 0 <= x <=1. You can also do similar 2-d "s" with higher-order polynomials. The general procedure is to take the general n-th order polynomial function, then define n+1 boundary conditions; it's useful to take enough derivatives to help with your boundary conditions. Then solve those n+1 equations for the n+1 arbitrary parameters to complete your For example, the cubic "s" above is defined as

f(x) = a*x**3 + b*x**2 + c*x + d f'(x) = 3*a*x**2 + 2*b*x + 1*c f(0) = 0 # start at 0 f'(0) = 0 # want a flat slope f(1) = 1 # end at 1 f'(1) = 0 # but flat at this end, as well. solve: d = 0 # from f(0) c = 0 # from f'(0) b = 3, a=-2 # from f(1) and f'(1)

... and a quintic version would also define the second derivatives at 0 and 1 as 0. I've also done similar ones where I constrain the endpoints less, and instead move the center (so f(0.5) = 0.75 or f(0.8)=0.5 or similar). Some caveats is that it's really easy to accidentally define a function whose output goes beyond 0 or 1 while still inside the range of x, so sometimes I use inequalities to bound the output, though that gets more difficult to solve.

This should be extensible into 3 dimensions:

f(x,y) = a*x**2 + b*y**2 + c*x*y + d*x + e*y + g f(0,0) = 0 f(1,1) = 1 df/dx(0,0) = 0 df/dy(0,0) = 0 df/dx(1,1) = 0 df/dy(1,1) = 0

(I don't know what shape that would make... I haven't gone thru and solved it). You would need to define boundary conditions that made sense for your HSV.

Actually, maybe it's simpler than that: maybe you just want a parameter "p", which maps through three separate functions

p:0..1 H(p) = a*p**3 + b*p**2 + c*p**1 + d I would recommend corners like: H(0) = Hmin H'(0) = 0 H(1) = Hmax H'(1) = 0 to get a similar "s" shape to 3*x**2 - 2*x**3, but scaled so the o +utput go from the min to the max you want S(p) = e*p**3 + f*p**2 + g*p**1 + h S(...) = Smin, 0, Smax, 0 V(p) = j*p**3 + k*p**2 + m*p**1 + n V(...) = Vmin, 0, Vmax, 0

If you change the p=0..1 domain to whatever matches your domain of your gold scale (ie, if you determine the shade of gold by the height on your screw, or at least the height between screw threads -- I forget which from your previous discussion -- then your p would be equal to your height, and you would just define the p corners as height-min and height-max instead of 0 and 1). Thus, with a linear progression thru your p-parameter, you should get a nice "s"-shaped progression for each of your H, S, and V.

--

*: technically, a true Sigmoid function will asymptotically approach 0 and 1 off toward infinite-x, and will be centered at x=0. This "polynomial sigmoid" was chosen back in Perlin's early days as a "good enough approximation" that was easy to compute/pre-compute on his old 80s computers.

**: I think I first encountered either in my neural network class back in the 90s, or when looking into Perlin noise for fun sometime thereafter.

EDIT 1: fixed the H,S,V cubics' last terms...

EDIT 2: change the last term of f(x,y) from an f (confusing) to a g (unambiguously different than f(x))

Replies are listed 'Best First'.
Re^2: A data selection problem(in3D).
by pryrt (Abbot) on Apr 06, 2017 at 15:17 UTC

    Ignore my f(x,y) suggestion: that gives a surface in 3d space. What you want is a line in 3d space. To get that, use the (x,y,z) = (H(p),S(p),V(p)) parameterization.

Re^2: A data selection problem(in3D).
by BrowserUk (Patriarch) on Apr 07, 2017 at 05:47 UTC

    pryrt Thank you very much indeed. Thanks to your leads; I've ended up finding the Logistic function, which with its tunable slope and rotational symmetry is exactly the function I was looking for, despite that I didn't know it existed :)

    I'm still getting to grips with how the tunable parameter(s) affect the curve; and I need to deal with the fact that at steep slopes, the y values stay hard at the bottom and top of the range for too long -- I think I'll need to fiddle with x-range to ensure that I start and finish at the beginning and end of the curves rather than well before and after; but I think I can handle that.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: A data selection problem(in3D).
by BrowserUk (Patriarch) on Apr 06, 2017 at 16:29 UTC

    Thanks. That looks like the way to go. I'll post whatever comes out of it. Thanks.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: A data selection problem(in3D).(Dammit! New requirement.)
by BrowserUk (Patriarch) on Apr 07, 2017 at 22:49 UTC

    Having found the Logistic function; played with the parameters and understood their effects, I went to apply it to my problem and realised that I had reached the wrong conclusion about how I needed to curve my way through the dataset.

    In a nutshell, rather than this parameterised curve, in need this one (produced by rotating the first left 90° and then flipping the image vertically.)

    I've proved (to myself) that I have no intuition for what maths is likely to produce such results; if you have any clues to this new requirement I'd be a great help?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Suggestion 1: given a parameter p that goes from -1 to 1, non-inclusive, you could do f(p) = tan(p*pi/2), but that doesn't give much control of the slopes. Given the logistic(p,L,K) = L / (1 + exp(-k*p)), you could do something like f(p) = tan( (2*logistic(p,L,K)-1)*pi/2 ), which allows some tuning... but I'm still not sure it's really tunable enough

      Even before this additional wrinkle, I was cogitating that maybe going the route of a Bezier curve would be the right way to go: given a set of k coordinates P_i for i=(0..k-1), where each coordinate is in n-dimensional space, you could use a (k-1)th-order Bezier to "get near" each of those k points (exactly hitting the two endpoints). For example, if your k=5 coordinates were the (H,S,V) for white, yellow, bright gold, dark gold, black -- then you could do a quartic Bezier (the wp article goes to cubic, but quartic would just be f(t) = (1-t)**4 * P0 + 4*(1-t)**3 * t * P1 + 6*(1-t)**2 * t**2 * P2 + 4*(1-t) * t**3 * P3 + 1*t**4 * P4. Or it might be easier to do a piecewise quadratic or cubic Bezier. The benefits of various Bezier is you can plop those points anywhere (you can make a circle out of four piecewise Beziers) to make highly arbitrary points... and you can tune them to get wonderfully sharp slopes.

        Suggestion 1: given a parameter p that goes from -1 to 1, non-inclusive, you could do f(p) = tan(p*pi/2), but that doesn't give much control of the slopes. Given the logistic(p,L,K) = L / (1 + exp(-k*p)), you could do something like f(p) = tan( (2*logistic(p,L,K)-1)*pi/2 ), which allows some tuning... but I'm still not sure it's really tunable enough

        Thanks. I'll take a look at that.

        I'm currently playing with tanh(), which produces a knee that bends in the right direction; and the slope can be tuned by simple scaling, but...as it contains a division, 1.0 has to be special cased to avoid divide by zero; and as it is basically log, you have to remove then put back the negative to produce the 'other half' of the curve.

        The latter is simple if a little messy; the former is a right royal pain the arse.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
        In the absence of evidence, opinion is indistinguishable from prejudice.

        After going round and around trying to get tan()/tanh() to do what I wanted, I ended up with using (relatively) simple exponentiation:

        sub myCurve { my( $x, $p ) = ( @_, 5 ); my $sgn = $x <=> 0; $sgn * abs( $x )**$p; }

        The jiggery pokery with the sign is so that I can supply input in the range -1 .. 1 and get the rotational symmetry I wanted.

        Its effect is demonstrated in this image. You'll probably need to zoom in to read the text and see the effect clearly.

        The top 10 pixel band, labelled "X->Y", is a simple linear path through the relevant hsv space; and the second band ("X**1") is identical but with the coordinates passed through my mapping algorithm with the exponent set to 1 to check it follows the same path.

        The third band "x**2" is the input coordinates passed through the mapping with the exponent set to 2. This shows the effect I was after, that of reducing the near black and near white ~3rds at either end and stretching the middle 3rd to fill the space.

        The other bands are higher exponents showing that the effect is configurable to a high degree. It looses some of the black and white at either end due to truncation to integer pixels which I can probably live with, or I could just replace the highest and lowest values in the gradient with black and white.

        This is only curving the gradient in 2D at the moment; I've still to get the mapping right for the third dimension (hsv-V), but I'll get there.

        Thanks for your input.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
        In the absence of evidence, opinion is indistinguishable from prejudice.