Error: function undefined

I have worked with user defned functions before but in this case I just can’t figure out what am doing wrong. I get the error "deCasteljauPoints not defined". Where is the error in my code?

function deCasteljauPoints!(points, t)
        res = []
        res.append(points)
        while length[points]>=2
            a=range(-30,stop=-1, length=100)
            b=range(1,stop=30, length=100)
            points = [(1-t) * p1 + t * p2 for (p1, p2) in zip(points(a,b))]
            res.append(points)
        return res
        end
end

points = [(-2.0, 2.0), (-3.0, 3.0), (-1.0, 2.0), (2.0, 3.0), (-1.0, 3.0)]
points = Array(points)
t=0.7
Bp=deCasteljauPoints(points,t)
Bp

You are defining your function with an added exclamation mark, but calling it without?

2 Likes

I’m not sure what you want your code to do, but here are a few things I noticed:

  1. points = Array(points) does not appear to be necessary
  2. length[points] should be length(points) because length is a function
  3. You probably want to omit res = [ ] and use append!(points, newPoints) to add new values to points.
  4. If you intend to modify points (adding ! to a function is the convention for mutation), you do not need to return anything. Otherwise, you probably want the return statement outside of your while loop.
2 Likes

It’s conventional in Julia for mutating functions to return something, usually the mutated argument. This allows the function call to be chained as an argument to another function.

3 Likes

Thanks for clarifying. I didn’t realize that functions like push! return the mutated object. I can see how that would be useful for chaining.

I made the appropriate corrections and did some more reading. What I want my code to do is to simply give me a list of points in between a range a & b within a time t.
How do I correctly define newPoints assuming I need to define it.
The error I have now is cannot `convert` an object of type Array{Tuple{Float64,Float64},1} to an object of type Tuple{Float64,Float64} which am thinking stems from my points being listed as an array of points.

Here is the code:

function deCasteljauPoints(points, t)
        append!(points, newPoints)
        while length(points)>=2
            a=range(-30,stop=-1, length=100)
            b=range(1,stop=30, length=100)
            points = [(1-t) * p1 + t * p2 for (p1, p2) in zip(points(a,b))]
            append!(points, newPoints)
            end
            return points
end
points = [(-2.0, 2.0), (-3.0, 3.0), (-1.0, 2.0), (2.0, 3.0), (-1.0, 3.0)]
#newPoints=?
t=0.7
B=deCasteljauPoints(points,t)
B

Please make sure to reduce your code to a minimum working example (MWE) that reproduces the error that you’re seeing and that others can run, have a look at: Please read: make it easier to help you

If I copy the code in your post above, I get the error newPoints not defined, which occurs on the very first line of your deCasteljauPoints function (where you do append!(points, newPoints), without defining newPoints first)

3 Likes

How do I define newPoints? I dont know how to do that and I will first put the defined newPoints before calling it in my function.I read the link too,thankyou.

Well that seems to me to be more of a question on your application and intent rather than on how to code it? Nowhere in your code are newPoints declared, and it’s impossible to guess what the correct definition would be without understanding what you are actually trying to do.

Your code snippet quite frankly doesn’t make a lot of sense and there are a few potential problems in there, my best guess is that you’re trying to do something like

function getpoints(t) 
    a=range(-30,stop=-1, length=100) 
    b=range(1,stop=30, length=100)
    return [(1-t) * p1 + t * p2 for (p1, p2) in zip(a, b)]
end

But this is a pretty wild guess based on those parts of your original function which actually do something…

1 Like

Yes, it seems like you have a bit of confusion about the algorithm that you should figure out first. Are you trying to implement this? I’m not familiar with the algorithm, but I found this StackOverflow question and translated that Python function into Julia here:

function deCasteljau(points, t)
    tmp_points = copy(points)
    while length(tmp_points) > 1
        for k in 1:length(tmp_points)-1
            pnt = tmp_points[k] * (1 - t) + tmp_points[k+1] * t
            tmp_points[k] = pnt
        end
        tmp_points = tmp_points[1:end-1]
    end
    return tmp_points[1]
end
        
points = [[-2.0, 2.0], [-3.0, 3.0], [-1.0, 2.0], [2.0, 3.0], [-1.0, 3.0]]
t=0.7
deCasteljau(points,t)

I don’t think this is doing quite what you want your function to do (what are the ranges inside your loop for?), but maybe it will be helpful as a starting point…

1 Like