You are not logged in.

- Topics: Active | Unanswered

**gAr****Member**- Registered: 2011-01-09
- Posts: 3,479

Hi,

How do you get the first 100 squares with your favorite language?

In J, it's simple:

`(1+i.100)^2`

Range[100]^2 in Mm?

"Believe nothing, no matter where you read it, or who said it, no matter if I have said it, unless it agrees with your own reason and your own common sense" - Buddha?

"Data! Data! Data!" he cried impatiently. "I can't make bricks without clay."

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 95,946

Hi;

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

Offline

**gAr****Member**- Registered: 2011-01-09
- Posts: 3,479

Functional programming is very powerful once we get a hang of it.

"Believe nothing, no matter where you read it, or who said it, no matter if I have said it, unless it agrees with your own reason and your own common sense" - Buddha?

"Data! Data! Data!" he cried impatiently. "I can't make bricks without clay."

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 95,946

I agree, I am hoping I can convince Agnishom of that.

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

Offline

**gAr****Member**- Registered: 2011-01-09
- Posts: 3,479

Anybody will, once they realize how compact the code can get!

"Believe nothing, no matter where you read it, or who said it, no matter if I have said it, unless it agrees with your own reason and your own common sense" - Buddha?

"Data! Data! Data!" he cried impatiently. "I can't make bricks without clay."

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 95,946

Yes, he is a very bright guy and luckily he does not have lots of years of programming in procedural languages to make it difficult.

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

Offline

**gAr****Member**- Registered: 2011-01-09
- Posts: 3,479

That's right.

"Data! Data! Data!" he cried impatiently. "I can't make bricks without clay."

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 95,946

Did you see the new problem in Bafflers?

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

Offline

**gAr****Member**- Registered: 2011-01-09
- Posts: 3,479

Yes, I was trying a way to use only the formulas for sector and square, could not proceed that way.

"Data! Data! Data!" he cried impatiently. "I can't make bricks without clay."

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 95,946

Oh okay, you will get it.

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

Offline

bobbym wrote:

How do you get the first 100 squares with your favorite language?

Python Code:

`for i in xrange(1,101): print i**2`

Functional Way (Python):

`map(lambda x: x**2,xrange(1,101))`

Maxima Code:

`for a: 1 thru 100 do display(a^2);`

Functional Way (Maxima):

`makelist(k^2,k,1,100);`

Mathematica Code:

`Function[x,x^2] /@ Range[1,100]`

or

`Map[Function[x,x^2],Range[1,100]]`

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 95,946

Simpler is Range[100]^2.

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

Offline

But that is a matrix operation of which I know nothing.

he does not have lots of years of programming in procedural languages to make it difficult.

I've got atleast 7 years of Procedural experience.

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 95,946

Nope, that is not a matrix operation. You are thinking it is like m x m but it is not.

Range[100] generates a list from1 to 100. Range[100]^2 means square every element of the list. This is why loops are unnecessary. In functional languages commands work on entire lists as if they were single objects. This is more readable and saves the programmer the details of indexing an array.

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

Offline

Range[100]^2 means Range[100] multiplied with Range[100]. Which is multiplying the matrix [1,2,3,...100] with the matrix [1,2,3,...100]. That is how it works.

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 95,946

It is not a matrix, it is a list. Let me illustrate.

What do you think Range[10]^(1/2) does?

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

Offline

If you are thinking that InsertSomeFunction[Range[100]] is the same as InsertSomeFunction /@ Range[100] then you are wrong.

Proof. Assume the contrary.

See this counterexample: there is a difference between Speak[Range[100]] is not the same as Speak /@ Range[100]

It works in the other case because it is a matrix operation.

What do you think Range[10]^(1/2) does?

It multiplies the matrix {1,2,3,4,5,6,7,8,9,10} with the constant 1/2

*Last edited by Agnishom (2014-03-02 01:37:32)*

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 95,946

It is not a matrix, it is a list. Let me illustrate.

What do you think Range[10]^(1/2) does?

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

Offline

What do you think Range[10]^(1/2) does?

It multiplies the matrix {1,2,3,4,5,6,7,8,9,10} with the constant 1/2

A matrix and a list is the same thing

*Last edited by Agnishom (2014-03-02 01:40:22)*

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 95,946

That is incorrect.

`Range[10]^(1/2)`

`Range[10]^n`

See how it was mapped to each element. Single brackets are lists.

`Sin[Range[10]]`

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

Offline

That is incorrect.

Sorry, I meant that it raises the matrix {1,2,3,4,5,6,7,8,9,10} to the power 0.5

Then why isn't Speak[Range[100]] and Speak /@ Range[100] the same?

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 95,946

Speak does that because it treats the argument as a string. Just reading what it sees. Because for most functions have the attribute called listable. That means that they map themselves over lists.

{1,2,3,4,5,6,7,8,9,10} is not a matrix, it is a list. In M, matrices are lists inside of lists. {{1,2,3},{4,5,6},{7,8,9}} that is a matrix. You see, in M everything is a list just like lisp.

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

Offline

Is it okay to say that for any list L, f[L] does the same thing as f /@ L?

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 95,946

Yes, in all cases that I have seen that will be true. The \@ means map. Just like a mapping in mathematics. It maps the function f onto all the elements of the list.

Please do not get confused here. In M, the list is used to represent vectors, matrices, sets, and arrays. It depends on the context and what commands you are using afterward whether you are speaking of a list or a vector.

**In mathematics, you don't understand things. You just get used to them.**

**If it ain't broke, fix it until it is.**

Offline

Okay, can we get back to explaining that code?

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

'But our love is like the wind. I can't see it but I can feel it.' -A Walk to remember

Offline