Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6181267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:59:23+00:00 2026-05-24T00:59:23+00:00

I am still not good working with lists in Mathematica the functional way. Here

  • 0

I am still not good working with lists in Mathematica the functional way. Here is a small problem that I’d like to ask what is a good functional way to solve.

I have say the following list made up of points. Hence each element is coordinates (x,y) of one point.

a = {{1, 2}, {3, 4}, {5, 6}}

I’d like to traverse this list, and every time I find a point whose y-coordinate is say > 3.5, I want to generate a complex conjugate point of it. At the end, I want to return a list of the points generated. So, in the above example, there are 2 points which will meet this condition. Hence the final list will have 5 points in it, the 3 original ones, and 2 complex conjugtes ones.

I tried this:

If[#[[2]] > 3.5, {#, {#[[1]], -#[[2]]}}, #] & /@ a

but I get this

{{1, 2}, {{3, 4}, {3, -4}}, {{5, 6}, {5, -6}}}

You see the extra {} in the middle, around the points where I had to add a complex conjugate point. I’d like the result to be like this:

{{1, 2}, {3, 4}, {3, -4}, {5, 6}, {5, -6}}

I tried inserting Flatten, but did not work, So, I find myself sometimes going back to my old procedural way, and using things like Table and Do loop like this:

a = {{1, 2}, {3, 4}, {5, 6}}
result = {};
Do[

 If[a[[i, 2]] > 3.5, 
   {
    AppendTo[result, a[[i]]]; AppendTo[result, {a[[i, 1]], -a[[i, 2]]}]
   }, 
   AppendTo[result, a[[i]]]
 ],
 {i, 1, Length[a]}
 ]

Which gives me what I want, but not functional solution, and I do not like it.

What would be the best functional way to solve such a list operation?

update 1

Using the same data above, let assume I want to make a calculation per each point as I traverse the list, and use this calculation in building the list. Let assume I want to find the Norm of the point (position vector), and use that to build a list, whose each element will now be {norm, point}. And follow the same logic as above. Hence, the only difference is that I am making an extra calculation at each step.

This is what I did using the solution provided:

a = {{1, 2}, {3, 4}, {5, 6}}

If[#[[2]] > 3.5, 
   Unevaluated@Sequence[ {Norm[#], #}, {Norm[#], {#[[1]], -#[[2]]}}], 
   {Norm[#], #}
 ] & /@ a

Which gives what I want:

{    {Sqrt[5],{1,2}}, {5,{3,4}}, {5,{3,-4}}, {Sqrt[61],{5,6}}, {Sqrt[61],{5,-6}}   }

The only issue I have with this, is that I am duplicating the call to Norm[#] for the same point in 3 places. Is there a way to do this without this duplication of computation?

This is how I currently do the above, again, using my old procedural way:

a = {{1, 2}, {3, 4}, {5, 6}}
result = {};
Do[
 o = Norm[a[[i]]];
 If[a[[i, 2]] > 3.5, 
  {
   AppendTo[result, {o, a[[i]]}]; AppendTo[result, {o, {a[[i, 1]], -a[[i, 2]]}}]
  }, 
  AppendTo[result, {o, a[[i]]}]
 ],
 {i, 1, Length[a]}
]

And I get the same result as the functional way, but in the above, since I used a temporary variable, I am doing the calculation one time per point.

Is this a place for things like sow and reap? I really never understood well these 2 functions. If not, how would you do this in functional way?

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:59:23+00:00Added an answer on May 24, 2026 at 12:59 am

    One way is to use Sequence.

    Just a minor modification to your solution:

    If[#1[[2]] > 3.5, Unevaluated@Sequence[#1, {#1[[1]], -#1[[2]]}], #1] & /@ a
    

    However, a plain ReplaceAll might be simpler:

    a /. {x_, y_} /; y > 3.5 :> Sequence[{x, y}, {x, -y}]
    

    This type of usage is the precise reason Rule and RuleDelayed have attribute SequenceHold.

    Answer to update 1

    I’d do it in two steps:

    b = a /. {x_, y_} /; y > 3.5 :> Sequence[{x, y}, {x, -y}]
    {Norm[#], #}& /@ b
    

    In a real calculation there’s a chance you’d want to use the norm separately, so a Norm /@ b might do

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still working on a particular RDF file, but seems that something is not
I'm still working on a good solution to my One-Of-A-Type Container Problem -- and
I still feel C++ offers some things that can't be beaten. It's not my
I've been working at this all morning and I still can't find a way
After reading Evan's and Nilsson's books I am still not sure how to manage
I just read the Wikipedia article on mock objects , but I'm still not
If not, is smoke testing still used?
I'm still having a hard time not wanting to use Tables to do my
Still 'diving in' to Python, and want to make sure I'm not overlooking something.
Not too practical maybe, but still interesting. Having some abstract question on matrix multiplication

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.