How do I properly substitute a variable for a fraction using Maple - substitution

I'm wondering why the first substitution does not work, the second, however, does.

g := q*N__1/N:
algsubs(N__1/N = X__1, g);
q*X__1
(I gave a detailed explanation in an answer to your question here.)

Related

Integer ceil(sqrt(x))

The answer gives the following code for computing floor(sqrt(x)) using just integers. Is it possible to use/modify it to return ceil(sqrt(x)) instead? Alternatively, what is the preferred way to calculate such value?
Edit: Thank you all so far and I apologise, I should have make it more explicit: I was hoping there is more "natural" way of doing this that using floor(sqrt(x)), possibly plus one. The floor version uses Newton's method to approach the root from above, I thought that maybe approaching it from below or similar would do the trick.
For example the answer even provides how to round to nearest integer: just input 4*x to the algorithm.
If x is an exact square, the ceiling and the floor of the square root are equal; otherwise, the ceiling is one more than the square root. So you could use (in Python),
result = floorsqrt(x)
if result * result != x:
result += 1
Modifying the code you linked to is not a good idea, since that code uses some properties of the Newton-Raphson method of calculating the square root. Much theory has been developed about that method, and the code uses that theory. The code I show is not as neat as modifying your linked code but it is safer and probably quicker than making a change in the code.
You can use this fact that:
floor(x) = (ceil(x) - 1) if x \not \in Z else ceil(x)
Hence, check if N is in the form 2^k, the code is the same, and if it is not, you can -1 the result of the current code.

R: Creating a function with an arbitrarily long expression

Although my original question is more general, in order to keep things more comprehensive, I'm formulating below just its partial case, - I expect that a solution/ answer for it will serve as an answer for the more general question.
Question:
to integrate a function f(x)=(...(((x^x)^x)^x)...^x)^x (... powered x n times) on the interval (0,1) ?
Thanks a lot for any ideas!
P.S.: please, do not try to solve the problem mathematically or to simplify an expression (e.g., to approximate the result with a Taylor expansion, whatever), since it's not the main topic (however, I've tried to choose such an example, which should not have any simple transformations)
P.S.2: Original question (which does not require an answer here, since it's expected that an answer for posted question is valid for original one):
if it's possible in R to create a function with an arbitrarily long expression (avoiding "manual" defining). For example, it's easy to set up manually a given function for n=5:
f<-function(x) {
((((x^x)^x)^x)^x)^x
}
But what if n=1'000, or 1'000'000 ?
It seems that simple looping is not appropriate here...
Copied from Rhelp: You should look at:
# ?funprog Should have worked but didn't. Try instead ...
?Reduce
There are several examples of repeated applications of a functional argument. Also composition of list of functions.
One instance:
Funcall <- function(f, ...) f(...) # sort of like `do.call`
Iterate <- function(f, n = 1)
function(x) Reduce(Funcall, rep.int(list(f), n), x, right = TRUE)
Iterate(function(x) x^1.1, 30)(1.01)
#[1] 1.189612

Alternate recursion function definition syntax in Mathematica

Suppose I wish to define a recursive function theta whose functionality should be apparent below.
The following definition will work.
theta[0] = 0;
theta[i_ ] := theta[i-1] + 1
However, this will not work.
theta[0] = 0;
theta[i_ + 1] := theta[i] + 1
My question is, is it possible to make something like the second definition work, where I can define the function based on the i+1 term instead of the i term?
I understand that they are mathematically equivalent, but I am curious about whether Mathematica will permit something like the second syntax.
It is perfectly feasible to make your second definition work if you understand that default automatic simplifications are done, often before you can get control, and if you then use your definition with appropriate parameters that match your definition.
Example
In[1]:= theta[i_ + 1] := Sin[i]+1;
theta[a + 1]
Out[2]= 1+Sin[a]
but then you probably expect to use this as
In[3]:= theta[8]
Out[3]= theta[8]
and that fails because you defined a function that matches the sum of something and one, but gave it just an integer and you have no definition that matches that. Even this fails
In[4]:= theta[7 + 1]
Out[4]= theta[8]
because the default automatic rules turn the sum of two integers into an integer and you are back to the previous case.
It is sometimes said that Mathematica does "structural" matching, if two structure of two expressions match the Mathematica accepts this as a match. This is very different from the sort of matching that anyone with a bit of mathematical maturity would use. A decade or more ago someone wrote up an article in the Mathematica Journal showing that it would be possible to use a more mathematical version of matching within Mathematica. I think that was completely ignored and nothing more was ever done with that. It would be nice if someone with the skill needed could bring that code up to the current version of Mathematica, but I think this might be a substantial challenge.
There is always "a way". For example:
ClearAll[a];
a[i_] = a[i] /. First#RSolve[{a[i + 1] == a[i] + 1, a[0] == 0}, a[i], i]

What is the command of output superfunction in mathematica?

I have two questions:
What is the command for a superfunction in Mathematica?
What is the difference between above superfunction and function in superspace which is odd variables times ordinary function? Are they the same thing?
In wikipedia, a superfunction S(z) of f is defined as S(z)=f(f(f(...f(t)))) (z total f's).
At 1: The command you're probably looking for is Nest:
In[1]:= Nest[f, x, 3]
Out[1]= f[f[f[x]]]
At 2: This doesn't look like a question for the Mathematica tag. It seems more physics/math related. I have added tags accordingly.
To address part 2 of your question:
Besides the prefix super-, there is no relation between superfunction (meaning an iterated function) and functions in super vector spaces.
Also, a superfunction is normally defined to be a map from a superspace to a supernumber, it does not have to be an odd element as stated in your question. See, e.g., section 1.10 of Ideas and methods of supersymmetry and supergravity.

Mathematica Map question

Original question:
I know Mathematica has a built in map(f, x), but what does this function look like? I know you need to look at every element in the list.
Any help or suggestions?
Edit (by Jefromi, pieced together from Mike's comments):
I am working on a program what needs to move through a list like the Map, but I am not allowed to use it. I'm not allowed to use Table either; I need to move through the list without help of another function. I'm working on a recursive version, I have an empty list one down, but moving through a list with items in it is not working out. Here is my first case: newMap[#, {}] = {} (the map of an empty list is just an empty list)
I posted a recursive solution but then decided to delete it, since from the comments this sounds like a homework problem, and I'm normally a teach-to-fish person.
You're on the way to a recursive solution with your definition newMap[f_, {}] := {}.
Mathematica's pattern-matching is your friend. Consider how you might implement the definition for newMap[f_, {e_}], and from there, newMap[f_, {e_, rest___}].
One last hint: once you can define that last function, you don't actually need the case for {e_}.
UPDATE:
Based on your comments, maybe this example will help you see how to apply an arbitrary function:
func[a_, b_] := a[b]
In[4]:= func[Abs, x]
Out[4]= Abs[x]
SOLUTION
Since the OP caught a fish, so to speak, (congrats!) here are two recursive solutions, to satisfy the curiosity of any onlookers. This first one is probably what I would consider "idiomatic" Mathematica:
map1[f_, {}] := {}
map1[f_, {e_, rest___}] := {f[e], Sequence##map1[f,{rest}]}
Here is the approach that does not leverage pattern matching quite as much, which is basically what the OP ended up with:
map2[f_, {}] := {}
map2[f_, lis_] := {f[First[lis]], Sequence##map2[f, Rest[lis]]}
The {f[e], Sequence##map[f,{rest}]} part can be expressed in a variety of equivalent ways, for example:
Prepend[map[f, {rest}], f[e]]
Join[{f[e]}, map[f, {rest}] (#Mike used this method)
Flatten[{{f[e]}, map[f, {rest}]}, 1]
I'll leave it to the reader to think of any more, and to ponder the performance implications of most of those =)
Finally, for fun, here's a procedural version, even though writing it made me a little nauseous: ;-)
map3[f_, lis_] :=
(* copy lis since it is read-only *)
Module[{ret = lis, i},
For[i = 1, i <= Length[lis], i++,
ret[[i]] = f[lis[[i]]]
];
ret
]
To answer the question you posed in the comments, the first argument in Map is a function that accepts a single argument. This can be a pure function, or the name of a function that already only accepts a single argument like
In[1]:=f[x_]:= x + 2
Map[f, {1,2,3}]
Out[1]:={3,4,5}
As to how to replace Map with a recursive function of your own devising ... Following Jefromi's example, I'm not going to give to much away, as this is homework. But, you'll obviously need some way of operating on a piece of the list while keeping the rest of the list intact for the recursive part of you map function. As he said, Part is a good starting place, but I'd look at some of the other functions it references and see if they are more useful, like First and Rest. Also, I can see where Flatten would be useful. Finally, you'll need a way to end the recursion, so learning how to constrain patterns may be useful. Incidentally, this can be done in one or two lines depending on if you create a second definition for your map (the easier way), or not.
Hint: Now that you have your end condition, you need to answer three questions:
how do I extract a single element from my list,
how do I reference the remaining elements of the list, and
how do I put it back together?
It helps to think of a single step in the process, and what do you need to accomplish in that step.

Resources