0
$\begingroup$

Here, I probably have a trivial case on how to use functions in Mathematica but I can't figure it out with the help of the Mathematica pages. I define the following function:

slope[x1_, x2_, y1_, y2_] := (y1 - y2)/(x1 - x2).

Then I want to evaluate this:

beta = slope[xp, xq, yp, yq].

I got:

Hold[beta = slope[xp, xq, yp, yq]],

which is definitely not what I wanted. I want to get $\frac{yp-yq}{xp-xq}$ out of it. ReleaseHold doesn't work. First I managed that by defining the function 'slope' without ':' in front of '='. After that, it wouldn't do that a second time so I tried defining the function with '$:=$' instead of only '='. Now nothing seems to work. Any helpful suggestion is highly appreciated.

EDIT: some function do their job now, magically ;-). But still, a new joke has risen. Begin to half of notebook:

In[1]:= phix[x_, y_] := y^2/x^2

In[2]:= phiy[x_, y_] := y (x^2 - b)/x^2

In[3]:= slope[x1_, x2_, y1_, y2_] := (y1 - y2)/(x1 - x2)

In[4]:= beta = slope[xp, xq, yp, yq]

Out[4]= (yp - yq)/(xp - xq)

In[5]:= linconst[x_, y_] := y - beta*x

In[6]:= gamma = linconst[xp, yp]

Out[6]= yp - (xp (yp - yq))/(xp - xq)

In[7]:= liney[x_] := beta*x + gamma

In[8]:= xr = beta^2 - (a + xp + xq)

Out[8]= -a - xp - xq + (yp - yq)^2/(xp - xq)^2

In[9]:= yp = liney[xp]

Out[9]= yp

In[10]:= yq = liney[xq]

During evaluation of In[10]:= $RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of -((xp (yp-yq))/(xp-xq)).

Out[10]= Hold[yp - (xp (yp - yq))/(xp - xq) + (xq (yp - yq))/(xp - xq)]

In[11]:= yr = liney[xr]

During evaluation of In[11]:= $RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of -((xp (yp-yq))/(xp-xq)).

Out[11]= Hold[yr = liney[xr]]

I have not defined any xp or xq. Only one Hold has remained. Could be the cases that I can't plug in variables in a function that already uses this variable as a constant.

EDIT 2: why doesn't Mathematica simply handle beta and gamma as constants and compute yq=beta*xq+gamma? Does it think that gamma and beta are functions or something?

$\endgroup$

closed as off-topic by Szabolcs, m_goldberg, Michael E2, MarcoB, Henrik Schumacher May 2 at 17:04

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Szabolcs, m_goldberg, Michael E2, MarcoB, Henrik Schumacher
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ Works fine for me. Did you try quitting your kernel andd trying again? You likely have some sort of lingering recursive definitions, because this is often what happens when you have that. So have you previously defined, say, xp in terms of beta? $\endgroup$ – march May 1 at 18:52
  • 1
    $\begingroup$ @Algeear. Yes exactly. You've got some recursive definitions going on somewhere, and you've got to suss out where it is. $\endgroup$ – march May 1 at 19:05
  • 1
    $\begingroup$ The code you originally posted was not a complete minimal example because it did not illustrate the issue. The problem is that you define gamma in terms of yp and yp in terms of gamma (indirectly through liney). It's the same as if you did a=b then b=a. $\endgroup$ – Szabolcs May 1 at 20:09
  • 1
    $\begingroup$ "but I miss some theory about defining functions" Have you tried searching the built-in documentation for "defining functions"? I suggest you go through the tutorial which is the first hit (called Defining Functions) then follow the Related Tutorials link at the end of that page. Ask a new question here if anything was unclear. $\endgroup$ – Szabolcs May 1 at 20:11
  • 1
    $\begingroup$ You might find both of these helpful: wolfram.com/language/fast-introduction-for-math-students/en wolfram.com/language/elementary-introduction/2nd-ed I did not read either (except for small bits), but you seem to be in the target audience. $\endgroup$ – Szabolcs May 2 at 14:20
5
$\begingroup$

Let's think about what your code is doing. At the time liney is first called (in your yp definition):

enter image description here

You call this with xp. In this functional form, everything but the yp cancels, so liney[xp] already equals yp. Therefore your Set statement is essentially yp=yp.

Now you call liney[xq]. The result is:

enter image description here

But now you assign that to yq, which is in the expression itself. This is the source of your recursion.

To see a simpler example, try executing this simple statement:

i=i+1

Mathematica is smart enough to recognize that the assignment of i+1 to i means that you have changed the right-hand-side, and so it needs to reevaluate it and reassign it to the left-hand side. And this starts a sequence that never terminates.

You may be thinking of beta and gamma as constants, but they aren't. They are expressions that depend on xp, xq, yp, and yq. So when you use them later in a context that also uses xp, xq, yp, and/or yq, Mathematica treats these variables the same way throughout.

Having gone through this, it is somewhat hard for me to see what it is you are actually trying to accomplish with this code. Are you intending xp, xq, yp, and yq to mean different specific values when you calculate beta and gamma vs. later when you call liney? If so, then use different names for them. Something like:

beta=slope[xpconst,xqconst,ypconst,yqconst]

If, on the other hand, you are trying to have xp, xq, etc. all mean the same thing all the way through, then you have defined a recursive problem, and Mathematica is simply doing exactly what you are telling it to.

$\endgroup$
  • $\begingroup$ Ah, thank you, that's very helpful. That way of recursion is a bit annoying but can be dodged by giving smart names. What I was trying to accomplish with this code is to show that phi is a homomorphism where I split up the map into the $x$- and $y$-transformation under the map $\varphi$. It was a supposed to be a short computation, but it didn't go as planned. This is used in the proof of Mordel's theorem and uses some map and I wanted to show that three points on a straight line are still on a straight line under that mapping $\varphi$. $\endgroup$ – Algebear May 1 at 23:13
  • $\begingroup$ The "problem" is that in the end I want one and the same name for all xp, yp etc. so using two names for one constant is not an option, I think. I will try better. $\endgroup$ – Algebear May 1 at 23:15
  • $\begingroup$ So perhaps what you want to do is solve a system of equations for the conditions where yp=liney[xp] and yq=liney[xr]? If that's it, try Reduce[{yp==liney[xp],yq==liney[xr]}] $\endgroup$ – Kevin Ausman May 2 at 17:25
  • $\begingroup$ The problem was that it made no sense at all to define yq=liney[xq]=yq since that wouldn't help any reduction of a certain expression because it's a tautology. I still have problems with my code since I want to simply check whether a specific point is on a line after some transformation but plugging the point $R=(XR,YR)$ ($r=(xr,yr)$) after applying phix and phiy) in the new line $y=\delta x+\eta$, on which $YP$ and $YQ$ lie, gives $YR-(\delta XR+\eta)\neq0$. $\endgroup$ – Algebear May 2 at 17:37
  • $\begingroup$ I try to restrict myself a lot since explaining the problem in great detail will result in too much unnecessary reading work for others. If you're interested, I posted the problem in its mathematical context on Math Stack Exchange: math.stackexchange.com/questions/3211336/… . It really does not require high maths but is rather a simple geometry problem which Mathematica does not solve well enough for me or I may be missing something.. $\endgroup$ – Algebear May 2 at 18:22
0
$\begingroup$

Edit: This "answer" is incorrect. I am leaving it so as to not look like I am removing my mistakes. Another answer to follow momentarily.

Original:

Not truly an "answer," but this allows me to post an image.

The code you provided does not produce the errors you reported when I execute it in Mathematica version 11.3:

enter image description here

$\endgroup$
  • $\begingroup$ Thanks for the time. You changed input line 9. It should read yp=liney[xp]. And then the other y's give me trouble. $\endgroup$ – Algebear May 1 at 20:58
  • $\begingroup$ Thank you for the correction. I can indeed duplicate your output now. I will follow-up in a moment. $\endgroup$ – Kevin Ausman May 1 at 22:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.