Why Terraform uses the expression "Interpolation" - terraform

So, mathematically speaking, interpolation is a method of constructing new data points within the range of a discrete set of known data points.
Now, Terraform has been using this terminology to reference values, like variables.
I can't seem to find any minimal coincidence using this math term in TF. Where is the match?

The other answers explain why Terraform uses the term interpolation, so I'll just answer the bit
Where is the match?
As you say, in mathematics, interpolation means making a new data point in between existing data points. Crudely, this could be something like
1 2 3 _ 5 6
^
please find an appropriate value for here
In software, string interpolation is the process of putting something into a gap in a string. For example
"For the problem above, _ is the simplest value" <- 4
So the coincidence comes about if you frame interpolation as "filling a gap". It's certainly not the exact same thing, but that's how they're similar.

Different industries can use the same term in different ways. The usage of the term interpolation in software development is very common. The fact that mathematics uses the term in different ways does not really matter in the same way that they word "punch" means one thing in the boxing world and another in the metal working world.
String interpolation

It's used in the same meaning as elsewhere in programming. Typically mentioned in a phrase "String interpolation".
To quote Wikipedia:
In computer programming, string interpolation [...] is the process of evaluating a string literal containing one or more placeholders,
The reason Terraform mentions that is that whenever you define a value in Terraform, you can use quotes "" and within the quotes have one or more expressions, e.g. "ami-${var.image_id}". These expressions are evaluated and interpolated into the String.

Related

Naming a method that returns 0 for negative values

I'm writing such a method library. I can't seem to find a good name for it nor any reference of anyone having named such a function before.
What would be a good name for it?
I would call it a clipper as a noun and clip as a verb.
The operation you're describing would be a NegativeClip or SubzeroClip. Of course you could just have a generic function with two or three arguments. Depending on your needs I could see LowerBoundClip or FloorClip or BottomClip being paired with UpperBoundClip or CeilingClip or TopClip instead. In a couple of those the word clip almost sounds redundant.
Words like bound, bounds, bounded, boundary are used in mathematics but as I think about possible function names I'm not sure they were as clear in meaning. And binding is already used in other programming topics so there's some potential for confusion that also.

If Non Terminals in Gene Expression Programming are mono-type functions, how to build complex Programs?

It just seemed to me studying GEP,and especially analyzing Karva expressions, that Non Terminals are most suitable for functions which type is a->a for some type a, in Haskell notation.
Like, with classic examples, Q+-*/ are all functions from 'some' Double to 'a' Double and they just change in arity.
Now, how can one coder use functions of heterogeneous signature in one Karva expressed gene?
Brief Introduction to GEP/Karva
Gene Expression Programming uses dense representations of a population of expressions and applies evolutionary pressure to make better ones to solve a given problem.
Karva notation represents an expression tree as a string, represented in a non-traditional traversal of level-at-a-time, left-to-right - read more here. Using Karva notation, it is simple and quick to combine (or mutate) expressions to create the next generation.
You can parse Karva notation in Haskell as per this answer with explanation of linear time or this answer that's the same code, but with more diagrams and no proof.
Terminals are the constants or variables in a Karva expression, so /+a*-3cb2 (meaning (a+(b*2))/(3-c)) has terminals [a,b,2,3,c]. A Karva expression with no terminals is thus a function of some arity.
My Question is then more related to how one would use different types of functions without breaking the gene.
What if one wants to use a Non Terminal like a > function? One can count on the fact that, for example, it can compare Doubles. But the result, in a strongly typed Language, would be a Bool. Now, assuming that the Non terminal encoding for > is interspersed in the gene, the parse of the k-expression would result in invalid code, because anything calling it would expect a Double.
One can then think of manually and silently sneak in a cast, as is done by Ms. Ferreira in her book, where she converts Bools into Ints like 0 and 1 for False and True.
Si it seems to me that k-expressed genes are for Non Terminals of any arity, that share the property of taking values of one type a, returning a type a.
In the end, has anyone any idea about how to overcome this?
I already now that one can use homeotic genes, providing some glue between different Sub Expression Trees, but that, IMHO, is somewhat rigid, because, again, you need to know in advance returned types.

Manipulating/Clearing Variables via Lists: Mathematica

My problem (in Mathematica) is referring to variables given in a particular array and manipulating them in the following manner (as an example):
Inputs: vars={x,y,z}, system=some ODE like x^2+3*x*y+...etc
(note that I haven't actually created variables x y and z)
Aim:
To assign values to the variables in the list "var" with the intention of inputting these values into the system of ODEs. Then, once I am done, clear the values of the variables in the array vars so that it is in its original form {x,y,z} (and not something like {x,1,3} where y=1 and z=3). I want to do this by referring to the positional elements of vars (I aim not to know that x, y and z are the actual variables).
The reason why: I am trying to write a program that can have any number of variables and ODEs as defined by the user. Since the number of variables and the actual letters used for them are unknown, it is necessary to perform manipulations with the array itself.
Attempt:
A fixed number of variables is easy. For the arbitrary case, I have tried modules and blocks, but with no success. Consider the following code:
Clear[x,y,z,vars,svars]
vars={x,y,z}
svars=Map[ToString,vars]
Module[{vars=vars,svars=svars},
Symbol[svars[[1]]]//Evaluate=1
]
then vars={1,y,z} and not {x,y,z} after running this. I have done functional programming with lists, atoms etc. Thus is makes sense to me that vars is changed afterwards, because I have changed x and not vars. However, I cannot get "x" in the list of variables to remain local. Of course I could put in "x" itself, but that is particular to this specific case. I would prefer to put something like:
Clear[x,y,z,vars,svars]
vars={x,y,z}
svars=Map[ToString,vars]
Module[{vars=vars,svars=svars, vars[[1]]},
Symbol[svars[[1]]]//Evaluate=1
]
which of course doesn't work because vars[[1]] is not a symbol or an assignment to a symbol.
Other possibilities:
I found a function
assignToName[name_String, value_] :=
ToExpression[name, InputForm, Function[var, var = value, HoldAll]]
which looked promising. Basically name_String is the name of the variable and value is its new value. I attempted to do:
vars={x,y,z}
svars=Map[ToString,vars]
vars[[1]]=//Evaluate=1
assignToName[svars[[1]],svars[[1]]]
but then something likeD[x^2, vars[[1]]] doesn't work (x is not a valid variable).
If I am missing something, or if perhaps I am going down the wrong path, I'm open to trying other things.
Thanks.
I can't say that I followed your train(s) of thought very well, so these are fragments which might help you to answer your own questions than a coherent and fully-formed answer. But to answer your final 'question', I think you may be going down some wrong path(s).
In passing, note that evaluating the expression
vars = {x,y,z}
does in fact define those three variables though it doesn't define any rewrite rules (such as values) for them.
Given a polynomial poly you can extract the variables in it with the function Variables[poly] so something like
Variables[x^2+3*x*y]
should return
{x,y}
Note that I write 'should' rather than does because I don't have Mathematica on this machine so my syntax may be a bit wonky. Note also that your example ODE is nothing of the sort but it strikes me that you can probably write a wrapper to manipulate an ODE into a form from which Variables can extract the variables. Mathematica offers a lot of other functions for picking expressions apart and re-assembling them, follow the trails from Variables. It often allows the use of functions defined on Lists on expressions with other heads too so it's always worth experimenting a bit.
There are a couple of widely applicable ways to avoid setting values of variables in Mathematica. For instance, you could write
x^2+3*x*y/.{x->2,y->3}
which will evaluate to
22
but not set values for x and y. This is a very simple example of using (sets of) replacement rules for temporary assignment of values to variables
The other way to avoid setting values for variables is to define functions using Modules or Blocks both of which define their own contexts. The documentation will tell you all about these two and the differences between them.
I can't help thinking that all your clever tricks using Symbol, ToExpression and ToString are a bit beside the point. Spend some time familiarising yourself with Mathematica's in-built functionality before going further down that route, you may well find you don't need to.
Finally, writing, in any language, expressions such as
vars=vars,svars=svars
will lead to madness. It may be syntactically correct, you may even be able to decrypt the semantics when you first write code like that, but in a week's time you will curse your younger self for writing it.

definition of variable

I ended up in a discussion with some friends about the definition of variable with respect to programming.
My understanding is that a variable in programming can be constant or changing.
Their opinion is that the real definition of the word variable is it can change, thus an identifier referring to some value which can change is a variable, where as a set of characters referencing a value which is defined as constant is literally called a constant. i.e.,
Int constant blah
Int argh
Thus by their definition they would refer to blah as a constant and argh as a variable.
My definition is would be the variable blah is constant and argh is also a variable (which is not constant)
Have I been referring to these identifiers incorrectly?
Your friends are correct. Constants and variables are essentially opposites by their definition.
A variable can represent many different values, and the value is unknown when referred to by name.
A constant on the other hand only represents one value at all times, and if you know it's value you can count on it never changing.
Of course in programming languages they are very similar things. They usually follow the same naming rules and can be stored the same way, but, just like variables aren't constants, constants aren't variables.
From my experience, it depends who you're talking to. That being said, my definition is
* A value is... a value (1, "a", etc)
* A variable is a name used to reference a value. It's possible to use multiple names to reference the same value, and for the value referenced by a variable to change over time, but neither is mandatory.
int a = 1;
^ variable
^ value
The wikipedia link mentioned by Cody Gray reinforces this view, or seems to in my opinion.
If it helps, consider that purely functional languages have variables but, by definition of being a functional language, the values that those variables point at cannot change over time.
It's also worth noting that your definition also depends on the context of your discussion. If you're talking about "variables vs constants", its reasonable to say they're polar opposites. If you're talking about "variables vs values vs keywords", you're talking about a different usage of the word variable (kind of).
As an example, consider fruit vs vegetable. In science terminology, an eggplant is a fruit. In culinary terminology, it's a vegetable. The culinary term vegetable can refer to things, in science terms, are fruits, roots, nuts, and a variety of other things. You need to know the context of your discussion to be able to say whether "x is a fruit" is accurate.

Identifying frequent formulas in a codebase

My company maintains a domain-specific language that syntactically resembles the Excel formula language. We're considering adding new builtins to the language. One way to do this is to identify verbose commands that are repeatedly used in our codebase. For example, if we see people always write the same 100-character command to trim whitespace from the beginning and end of a string, that suggests we should add a trim function.
Seeing a list of frequent substrings in the codebase would be a good start (though sometimes the frequently used commands differ by a few characters because of different variable names used).
I know there are well-established algorithms for doing this, but first I want to see if I can avoid reinventing the wheel. For example, I know this concept is the basis of many compression algorithms, so is there a compression module that lets me retrieve the dictionary of frequent substrings? Any other ideas would be appreciated.
The string matching is just the low hanging fruit, the obvious cases. The harder cases are where you're doing similar things but in different order. For example suppose you have:
X+Y
Y+X
Your string matching approach won't realize that those are effectively the same. If you want to go a bit deeper I think you need to parse the formulas into an AST and actually compare the AST's. If you did that you could see that the tree's are actually the same since the binary operator '+' is commutative.
You could also apply reduction rules so you could evaluate complex functions into simpler ones, for example:
(X * A) + ( X * B)
X * ( A + B )
Those are also the same! String matching won't help you there.
Parse into AST
Reduce and Optimize the functions
Compare the resulting AST to other ASTs
If you find a match then replace them with a call to a shared function.
I would think you could use an existing full-text indexer like Lucene, and implement your own Analyzer and Tokenizer that is specific to your formula language.
You then would be able to run queries, and be able to see the most used formulas, which ones appear next to each other, etc.
Here's a quick article to get you started:
Lucene Analyzer, Tokenizer and TokenFilter
You might want to look into tag-cloud generators. I couldn't find any source in the minute that I spent looking, but here's an online one:
http://tagcloud.oclc.org/tagcloud/TagCloudDemo which probably won't work since it uses spaces as delimiters.

Resources