Let's say we have two variables, eta and phi related by eta = cos(phi).

Is there a way to link these variables in any programming language such that there's no need for two different functions, phiToEta(phi) and etaToPhi(eta)?

  • 1
    I would say any solution will be more complex and harder to understand than having two conversion functions.
    – Euphoric
    Apr 14 '20 at 11:19
  • 1
    Sure, make them two properties of an object that manages the internal state and allows access interchangeably. Apr 14 '20 at 11:34
  • 1
    The question is, without conversion methods, how do you envisage converting from one to the other?
    – Steve
    Apr 14 '20 at 13:33
  • Are you talking about this case: phi = x; eta = cos(phi); phi = y; and then the eta variable is automatically refers to cos(y) (essentially)? Can you edit your question and provide pseudo code that illustrates your question? Apr 14 '20 at 16:47
  • Bear in mind that there's no guarantee that arccos(cos(x)) = x.
    – Simon B
    Apr 15 '20 at 16:05

This would be usually implemented by having two functions eta() and phi() that each access some common state, plus corresponding setters. The user wouldn't have to know how this state is stored internally, e.g. whether phi or eta is stored internally.

Where those are object-level fields, many languages also support some kind of property syntax, so that these functions can be triggered by accessing or assigning to object fields. JavaScript, Python, and C# have such features.

Swift is one of the rare languages that additionally supports computed local variables:

var phi: ... = ...
var eta: ... {
  get { return cos(phi) }
  set(newEta) { ... }

There are two things that spring to mind.

The first is referential transparency, which would mean the value eta is replaceable everywhere with cos(phi) or vice-versa, but I suspect this isn't quite what you are after.

The other is that for some functions the inverse function can be automatically derived (iirc the J languge provides this out of the box, probably other related languages too, also related SO question https://stackoverflow.com/questions/13404208/in-pure-functional-languages-is-there-an-algorithm-to-get-the-inverse-function)


Many languages can utilize closures and anonymous functions (sometimes called "lambda functions").

A closure in programming defines a scope for information in a program whereby an argument to an outer function remains available to an inner function after the outer function has finished execution. Anonymous functions, from the perspective of a programming language, are basically pointers to a function that is not necessarily a member of any class or data structure. The combination of closures and anonymous functions will probably get you what you want in conjunction with passing data by reference instead of value.

// receives pointer to a number
function cosOfPhi(&n) {
    return () => cos(n); // Dereference pointer to number

var phi = 4;
var eta = cosOfPhi(&phi); // Pass pointer to phi variable

print(eta()); // Prints result of cos(4)

phi = 5;

print(eta()); // Prints result of cos(5)

This isn't written in any particular language. The & sign being a fictional operator to pass an argument by reference, or receive a parameter by reference. Basially your eta variable is an anonymous function returned by the cosOfPhi function. By passing phi by reference reassigning phi in the main part of the program overwrites the memory the eta() function is referencing in its function closure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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