Mastering Mathematica Functions: Recursive Vector Squaring & List Sorting

  • Context: Mathematica 
  • Thread starter Thread starter Physics_rocks
  • Start date Start date
  • Tags Tags
    Functions Mathematica
Click For Summary
SUMMARY

The discussion focuses on creating recursive functions in Mathematica for squaring vector elements and sorting lists. The user initially attempted to define a squaring function using incorrect syntax and logic, particularly with the use of assignment and indexing. The correct approach involves using delayed assignment (:=) and proper list indexing with double brackets (list[[n - 1]]). Additionally, clarification on how to pass lists to functions was provided, emphasizing that the input list is defined prior to function calls.

PREREQUISITES
  • Understanding of Mathematica syntax and function definitions
  • Familiarity with recursive programming concepts
  • Knowledge of list manipulation in Mathematica
  • Basic understanding of delayed assignment in Mathematica (:=)
NEXT STEPS
  • Learn about recursive function design in Mathematica
  • Explore list indexing techniques in Mathematica
  • Study the use of delayed assignment (:=) in Mathematica functions
  • Investigate sorting algorithms and their implementation in Mathematica
USEFUL FOR

Mathematica users, programmers learning functional programming, and anyone interested in recursive algorithms and list processing in Mathematica.

Physics_rocks
Messages
12
Reaction score
0
Hi guys ,

I'm a little new with Mathematica and I'm trying to write down some functions.
the first one is writing a functions that square the elements in a vector , recursively ,
using patterns .

so , I tried this one :
x = Range[1, 10];
sq[0,list_]=list^2;
sq[sum_,list_] := Sum[sum*sq[sum, list[n - 1]], _list]


but it isn't working . what's the problem ? 2. The second function is writing a function that sorts a list of numbers , by checking each time two numbers that are adjacent .

if I write this : sort[list_] :=
how can I tell mathematica that I want to use an actual list of numbers ? can I say list ?

thank you
 
Physics news on Phys.org
There seem to be multiple problems.
First of all, the assignment in the second line should probably be delayed (:= instead of =) because you want to substitute "list" for whatever value is passed. Also, list[n - 1] is a function call, you meant list[[n - 1]]? Anyway, what is n in that expression? Finally, what are you doing with the "Sum" expression? Sum[f[x], {x, a, b}] sums the values of f[x] for a <= x <= b.

Can you maybe explain more clearly what you want the function to do (e.g. if you input x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, do you want it to output {1, 4, 9, 16, 25, 36, 49, 64, 81, 100} ?)

2) How do you mean: "use an actual list of numbers"? You can call sort[x] (where x is defined as in your first example). In the expression after ":=", the expression "list" will contain whatever list you input (so if you call sort[x], then the definition is executed where list = x).
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K