Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I currently teach myself programming and I came to a point where I was introduced to the recursive functions. I understand the basic principle behind them but whenever I try to read a code which contains recursive functions I have tough times tracing them. Honestly, If I don't write down the whole function on a paper and manually follow it I can't really understand what it does, I can't follow it mentally is what I am saying. Can you give some tips on how to do it mentally? I am pretty much average intelligent ( IQ 117 ), maybe that's the problem? Thanks in advance.
I think this is an excellent question. Many people make the mistake of trying to follow the call-stack of a recursive function, which is the wrong approach.
I find the only way is to look at what is written and ask yourself whether it is logically correct and whether it will end. If those things are true, it is going to work and you don't really need to understand how.
In more detail:
Are all possible input values considered?
Are the statements true in each case?
Is there a (are there) value(s) at which point the function will not recurse?
Do all remaining input values ultimately lead to that value?
For example (pseudo code):
factorial(n):
if (n < 0) throw InvalidArgument
if (n == 0) return 1
else return factorial(n - 1) * n
All input values are covered. The statements are true. There is a value at which it does not recurse (<=0) and the all remaining values reduce by 1 until the exit value is reached. Therefore it works.
You may of course make mistakes in your thinking. A good IQ does help here, and you may need at some point to resort to tracing or debugging to check your assumptions and find the error in your logic, but this should not be the first approach.
That is the beauty of recursive solutions: they tend to be statements of fact rather than procedural instructions!
I think IQ does not matter. Anyway, i think it's all about practice. Try to write some recursive functions. And after alot of practice and experience you will find it more easy to understand.
There is some good ideas.
Good luck.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Is the following code example, calling a function in the parameters line of another function, acceptable? Or will I run in problems somehow?
newfunction <- function(df = merge_two_df(), size = define_size()){
... do something
}
This is basically a question of what you gain and lose compared to the alternatives.
So first let's look at the alternatives:
Option1
newfunction <- function(){
df = merge_two_df(), size = define_size()
... do something
}
Option2
newfunction <- function(df,size){
... do something
}
newfunction(merge_two_df(),define_size())
Comparing your status-quo to option 1 shows that your current way is more flexible. You have defined default values for each parameter but retain the option to use other parameters. So there is nothing to gain hard-coding the parameter definition into the body of the function.
Now comparing to option 2, your current way is actually more standardized because you have defined defaults. You can simply and cleanly call your function without any parameters while having the same flexibility of switching up parameters if you want. However on the flip side you are a bit less explicit in calling these parameters which might be bad in terms of documentation and clarity when rereading the code.
If this function is part of a module that might be used/exported across several projects some even done by someone other then yourself, then I'd argue for option 2 to be simpler and clearer.
Otherwise what you do right now works best.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
My employer asked me to do a project for our local team. Actually, it will be a way to help our work to finished faster.
We have a local database where we add exercises divided in two fields. The question and the solution. My employer wants since we are a team and we work at the same time, to create a system like stackoverflow's similar questions. When one of the team tries to submit a new data in the database, then it will check if there are other fields which may be duplicates.
The reason he asked me is because I have done something similar in the past but only for text using techniques like TF-IDF and Latent Semantic Analysis. But now, since the math symbols are all in Latex, I cannot find a way to check for duplicates.
I have tried to apply TF-IDF to the text only, but it doesn't work.
Any suggestion?
Edit:
Sorry for the broad topic. I will try to give more examples about my problem.
All the texts are exercises of primary and secondary schools. It is a mix of text and numbers-equations-symbols. If there were only text, I could use TF-IDF to find possible duplicates. Now, several exercises have a little or are without text.
Examples:
1) a. Solve the following equation: (x+1)*(x-1) = 5
b. Find the x: x^2 - 1 = 5
They are the same equation but with a different expression. So, I don't want to mark them as duplicates.
2) a. Solve the following equation: 3x + 7 = 12
b. Find the solution: 7 + 3x = 12
c. Find the x: 3x = 12 - 7
a and b should be duplicate whereas the c will not be.
You could try using MathJax to convert the LaTeX equation into MathML an XML format. You could then use tools to examine that structure. There are probably a few other tools which can convert your equation into some kind of tree structure.
Equality of mathematical expressions is a complex problem. There are question that should you treat (x+1)*(x-1) as being equal to x^2-1, algebraically they are the same.
You might want to investigate computer algebra systems which have a lot of sophisticated features for manipulating expressions.
One technique is to evaluate the expression at a number of points. If the values agree then its a good indication that the expressions are the same.
It might be easier to give a better answer if there was some idea of the type of problems you are working with, polynomials, integrals etc.?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I would like R to be more pythonic. One of the steps for transforming it would be the use of 'not' instead of '!'. I hope this does not need writing of C++ and compiling etc, or does it?
Functionality: "!TRUE" should equal "not TRUE".
If you know where to start, please point me there.
You better embrace the R way. Why all the effort for such a small thing? If you want Python, stick with Python. Besides, it is REALLY common to have ! act as "not" in other programming languages.
Anyway, this is something that allows the use of "not", but it will involve parentheses (the Arrr! way)
not <- function(x) { !x }
if (not(FALSE)) { print(1) }
[1] 1
In my opinion this is far worse than !.
And I can hear you thinking "but I would like to have if x is not in y:", but please, that would be the same as suggesting I'd want a way for Python not to need indenting.
The idea is that you learn a language, you do not change a language to suit an individual's needs. What will happen with others using your code?
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
i'd like to know how to solve a definite integral in Mathematica.
I do know all variables except b, and need to solve for F(b)=0.
How can i solve it in Mathematica?
Here is my try:
NSolve[Integrate[1/(8*(1 - ff) (2 Pi)^0.5) E^(-0.5*((x - 1.1)/(1 - ff)/8)^2), {x, 0, 9999}] == -0.44531779637243296, ff]
These integrals can be trivially expressed in terms of an error function: Wiki, Mathworld. Hence what you need here is a library to (i) calculate error functions, (ii) numerically solve non-linear equations. Virtually any language has this, so pick anything you're familiar with. In Mathematica, look up Erf and NSolve.
I'd start by plugging it into Wolfram Alpha and see what it gives you.
Mathematica should be able to do it. I think of statistics first when R comes up; I don't know about its calculus capabilities. Excel is not the first choice.
If I were you, I'd be less worried about the software and more worried about the solution itself. A function of this form might be well known. Plot each one and visually check to see what the functions look like and how easy they might be to integrate.
Like this:
http://www.wolframalpha.com/input/?i=graph+exp%28-%28%28x%2B5%29%2F1.5%29%5E2%29
You should be wondering why it's three similar looking integrals. Those singularities in the plot tell you why.
If there's no closed form solutions, you'll have to go with a numerical one. You'll have to choose an algorithm (simple Euler or Runga Kutta or something else), interval sizes, etc. You'll want to know about singular points and how best to tackle them.
Choosing a package is just the start.
You might find http://r.789695.n4.nabble.com/calculus-using-R-td1676727.html helpful.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
Can someone point me to a good tutorial for using vectorized programming methods in R.
At the moment it feels very magical to me and I don't really understand what R's doing.
Especially with regards to if statements and addressing values neighboring rows.
I am not aware of a specific tutorial on vectorized programming for R.
I have a few versions of my Intro to High-Performance Computing with R tutorial here. The benefit of vectorized code is mentioned in the context of profiling, but it doesn't explain 'how to vectorize code'. I think that is hard to teach -- my best bet would be to read other people's code. Pick a few packages from CRAN and poke around.
Other than that, decent general purpose documents about R and programming in R are e.g. Pat Burns S Poetry and the more recent R Inferno.
The best way to learn this is to experiment with it since it's an interactive environment, and it's easy to create dummy data.
With regards to making comparisons in neighboring rows, the easiest thing to do is to use the - operator (which means "exclude this index") to eliminate the first and last row, as in this example:
a <- 1:10
a[5] <- 0
a[-1] > a[-length(a)] # compare each row with the preceding value
If you want to do an if statement, you have two options:
1) The if command only evaluates one value, so you need to ensure that it evaluates to TRUE/FALSE (e.g. use the all or any functions):
if(all(a[-1] > a[-length(a)])) {
print("each row is incrementing")
} else {
print(paste("the",which(c(FALSE, a[-1] <= a[-length(a)])),"th row isn't incrementing"))
}
2) You can do a vectorized if statement with the ifelse function. See help("ifelse") for more details. Here's an example:
ifelse(a[-1] > a[-length(a)], 1, 0)