SICP exercise 1.19 - sicp

It's a procedure to genearate the fibonacci numbers, here is the reference: http://sicp.org.ua/sicp/Exercise1-19
it's said that we can consider the procedure as "a <- bq + aq + ap and b <- bp + aq".My question is how the auther(or someone else) think out this good idea?Has it to be this form?

It's called the Fibonacci Q-Matrix, and the idea was "caught on like wildfire among Fibonacci enthusiasts. Numerous papers have appeared in Fibonacci Quarterly authored by Hoggatt and/or his students and other collaborators where the Q-matrix method became a central tool in the analysis of Fibonacci properties". You may refer to http://www.goldenmuseum.com/1505FibMatrix_engl.html and http://mathworld.wolfram.com/FibonacciQ-Matrix.html.

Related

Is there any way to do example based programming? [closed]

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 6 years ago.
Improve this question
Is it possible to automatically generate a program/function by writing a certain amount of examples which show the before and after? How many examples would be needed to insure correctness and lack of holes? What is the name of doing such an automatic process?
It's called program synthesis or perhaps guessing. Your question reminds me of the 2013 ICFP programming contest where you were supposed to guess programs and all you had was the output for your chosen test input in a very simple functional language.
So imagine you wanted to make the identity function:
(define-by-example identity
(0 -> 0)
(1 -> 1))
There is something missing? What if I told you to make a function that returns it's own value for 0 and 1 only? How would you make that different than the above example? What if it's a non linear transformation. Eg. how many examples does it take to get a polynomial correct?
I guess we're back to something like this:
(define-by-example fib
(0 -> 0)
(1 -> 1)
(n -> (+ (fib (- n 1)) (fib (- n 2)))))
But many languages has exactly this. Even Schemes dirty cousin Racket has this:
#!racket
(define (fib n)
(match n
[0 0]
[1 1]
[n (+ (fib (- n 1)) (fib (- n 2)))]))
This is the Hello World of Haskell so we better write that next :-)
-- haskell
fib 0 = 0
fib 1 = 1
fib n = fib(n - 1) + fib(n - 2)
Now there are smart people out there that tries to find the best method of transferring the idea down to a syntax that is clear for the computer and us. I'm sure the essence of "by example" is good but it needs to be a way of explaining the corrolations too and I cannot think of a better way to do that than maths or code where maths don't apply.
This is a subdomain of machine learning called supervised learning. The most popular method to solve these problems is by using neural networks. Another method is genetic programming.
In the general case it is not possible to guarantee correctness, no matter how large the training set (number of examples) is. But in a typical application this is not required. The training is considered successfull if a high enough percentage os the results is correct.
Is it possible to automatically generate a program/function by writing a certain amount of examples which show the before and after?
I'd say yes, but ...
... it's not exactly that easy, and certainly not that generally applicable. There are basically two "approaches" that I know of:
a) Try to generate a program
Just pack together program code (most probably instructions from some instruction set) until you get the desired results. This can be done with brute force, but it's hardly possible to synthesize non-trivial functions, yet alone complete programs, or probably with techniques from your favorite artificial intelligence tool set, like hill climbing, simulated annealing, genetic programming and the whole rest.
The only reference that I know of is the "Superoptimizer" by Henry Massalin, which tries to generate a function that computes the same as a given function, just with fewer instructions.
It's probably better to use some higher level representation of "computation" than assembler instructions (probably the Lisp AST?), but that's just my guess.
b) Write a meta program that "somehow" learns to behave like the desired program
This is actually very common nowadays in the form of neural networks, e.g. for image or voice recognition. Here you don't try to get a program that does what you want (e.g. "recognize birds in arbitrary images") but rather write a general program that is capable of learning to "behave differently" and train that in order to behave as you want.
Note that there's a lot of effort going into understanding what these meta programs actually do after they learned their intended function. Just taking the first relevant result from a Google search is already an interesting read.

free and bound identifiers in functional programming

I've been banging my head at this question for sometime and I can't figure it out. I've read the definition of free variable "Free variables and bound variables" from wikipedia and several books but I can't get the answer right
Consider the following code:
local A B C=1 D=2 in
A = 1
proc {Add E F G}
E = A + D + F
end
end
Which of these identifiers (A, B, C, D, E, F, G) are free identifiers?
The concept of free identifier comes always with a context. If you consider only the statement E=A+D+F the four identifiers are free. But if you consider the procedure definition, E and F are now bound because they are formal parameters. So the free identifiers are A and D. Finally if you consider the entire code you give, there is no free identifier since all identifiers are declared.
Reference : Concepts, Techniques and Models of Computer Programming by Peter Van Roy and Seif Haridi.
The end of page 57 and the page 58 are interesting for this matter.
The first three chapters are available on the edX platform if you enroll the course Paradigms of Computer Programming
The free identifiers of any instruction are those indentifier occurrences inside the instruction that correspond to declarations outside the instruction.
So it seams that A and D was the answer.

using unix time utility to find thetha equivalence class for quicksort

I used the time utility to calculate the user time for a quicksort algorithm with inputs of 10000,20000, ...,60000 words and here are the results I have
n( in thousands) T(n)
1 1.740
2 3.7
3 5.83
4 7.93
5 10.18
6 12.41
What I want to find out is f(n) such that T(n)= theta(f(n)) i.e., I need to guess f(n) such that T(n)/f(n) approaches a non-zero constant.
I tried the following f(n) functions but nothing seems to generate the constant
f(n) =n
f(n) = nlogn
f(n) = n+sqrt(n)
f(n) = n^2
f(n)=n + logn
f(n)=1/n
From what I inferred, T(n) has n as lower bound and n log n as upper bound. So I need a function between these two values. Please help.
You have a lot of possible options there. I would start by fitting your data to each of the six equations and seeing what how well the fits work. For instance, if you tried plotting your results you would immediately see that they are in a nearly straight line. Using any graphing software would help you see this. In science and mathematics, this is always a good idea: plot your results!
I was lazy, so I used Excel to fit a straight line to your data and I found the equation:
T(n) = 2.1379n - 0.524
with an R2 of 0.9995. Even Excel will give you these R2 values, to tell you how good the fit is to the data (you want R2 as close to 1 as possible). Now, this result is quite good, and you could stop there, but I thought I would try to fit your data to the rest of the equations and see what I got. I found that the best fit to your six functions was:
T(n) = 0.0327n<sup>2</sup> + 1.911n + 0.219
with an R2 of better than 0.999! Now THAT is a really good fit. Of course, if you want more accuracy, you should probably try this in Igor (which is free) instead of Excel. Especially since Excel has been known to give negative R2 values.
The take home message, I think is that you should always try plotting your results. It's so easy these days. After that, I think you were too concerned about re-inventing the wheel and deriving these fits yourself. There is plenty of software to do this for you.

FastICA and Blind Signal Separation Mathematics Question

I'm trying to make my own implementation of the FastICA algorithm based on the paper here: http://www.cs.helsinki.fi/u/ahyvarin/papers/NN00new.pdf.
I need some help w/ the math though.
In the middle of page 14 there is an equation that looks somewhat like
w+ = E{ xg(w^Tx) } - E{ g[prime]( w^T x)} w
What does the E mean? Back from my probability days I recall that it is the "expected value" of a random variable but it doesn't make sense to me what the expected value of a vector is.
Thanks,
mj
ICA is interesting stuff. I used it some in my graduate research, but I didn't dig in too much under the hood; I just downloaded the FastICA implementation for MatLab and used that.
Anyway, you are correct that E{...} denotes expected value. The elements of the vector x represent the individual signals. Strictly speaking, x is a time series and should be written x(t), but the convention in ICA is to treat x instead as a random variable. In that context, of course, the idea of expected value makes sense. For example E{x} would just be the mean value of x (taken to be zero in ICA as the signals have been centered).
The authors of the paper you linked also have a book on ICA. It's outrageously expensive on Amazon, but if you can find a copy at, say, a nearby university library, it might be worth a look. It's been several years, but I remember it as being as gentle an introduction as one could hope for given the mathematics.

Algebraic structure and programming

May anyone give me an example how we can improve our code reusability using algebraic structures like groups, monoids and rings? (or how can i make use of these kind of structures in programming, knowing at least that i didn't learn all that theory in highschool for nothing).
I heard this is possible but i can't figure out a way applying them in programming and genereally applying hardcore mathematics in programming.
It is not really the mathematical stuff that helps as is the mathematical thinking. Abstraction is the key in programming. Transforming real live concepts into numbers and relations is what we do every day. Algebra is the mother of all, algebra is the set of rules that defines correctness, it is the highest level of abstraction, so, understanding algebra means you can think more clear, more faster, more efficient. Commencing from Sets theory to Category Theory, Domain Theory etc everything comes from practical challenges, abstraction and generalization requirements.
In common practice you will not need to actually know these, although if you are thinking of developing stuff like AI Agents, programming languages, fundamental concepts and tools then they are a must.
In functional programming, esp. Haskell, it's common to structure programs that transform states as monads. Doing so means you can reuse generic algorithms on monads in very different programs.
The C++ standard template library features the concept of a monoid. The idea is again that generic algorithms may require an operation to satisfies the axioms of monoids for their correctness.
E.g., if we can prove the type T we're operating on (numbers, string, whatever) is closed under the operation, we know we won't have to check for certain errors; we always get a valid T back. If we can prove that the operation is associative (x * (y * z) = (x * y) * z), then we can reuse the fork-join architecture; a simple but way of parallel programming implemented in various libraries.
Computer science seems to get a lot of milage out of category theory these days. You get monads, monoids, functors -- an entire bestiary of mathematical entities that are being used to improve code reusability, harnessing the abstraction of abstract mathematics.
Lists are free monoids with one generator, binary trees are groups. You have either the finite or infinite variant.
Starting points:
http://en.wikipedia.org/wiki/Algebraic_data_type
http://en.wikipedia.org/wiki/Initial_algebra
http://en.wikipedia.org/wiki/F-algebra
You may want to learn category theory, and the way category theory approaches algebraic structures: it is exactly the way functional programming languages approach data structures, at least shapewise.
Example: the type Tree A is
Tree A = () | Tree A | Tree A * Tree A
which reads as the existence of a isomorphism (*) (I set G = Tree A)
1 + G + G x G -> G
which is the same as a group structure
phi : 1 + G + G x G -> G
() € 1 -> e
x € G -> x^(-1)
(x, y) € G x G -> x * y
Indeed, binary trees can represent expressions, and they form an algebraic structure. An element of G reads as either the identity, an inverse of an element or the product of two elements. A binary tree is either a leaf, a single tree or a pair of trees. Note the similarity in shape.
(*) as well as a universal property, but they are two of them (finite trees or infinite lazy trees) so I won't dwelve into details.
As I had no idea this stuff existed in the computer science world, please disregard this answer ;)
I don't think the two fields (no pun intended) have any overlap. Rings/fields/groups deal with mathematical objects. Consider a part of the definition of a field:
For every a in F, there exists an element −a in F, such that a + (−a) = 0. Similarly, for any a in F other than 0, there exists an element a^−1 in F, such that a · a^−1 = 1. (The elements a + (−b) and a · b^−1 are also denoted a − b and a/b, respectively.) In other words, subtraction and division operations exist.
What the heck does this mean in terms of programming? I surely can't have an additive inverse of a list object in Python (well, I could just destroy the object, but that is like the multiplicative inverse. I guess you could get somewhere trying to define a Python-ring, but it just won't work out in the end). Don't even think about dividing lists...
As for code readability, I have absolutely no idea how this can even be applied, so this application is irrelevant.
This is my interpretation, but being a mathematics major probably makes me blind to other terminology from different fields (you know which one I'm talking about).
Monoids are ubiquitous in programming. In some programming languages, eg. Haskell, we can make monoids explicit http://blog.sigfpe.com/2009/01/haskell-monoids-and-their-uses.html

Resources