10
$\begingroup$

Suppose I have a list like:

list1 = {{f[x], Sin[x]}, {f'[x]}, {f''[x]}};
list2 = {{f[x]}, {Cos[x], f''[x]}};

enter image description here

I want to pick these lists and convert them to:

list1 = {{f[x], Framed[Sin[x]]}, {f'[x]}, {f''[x]}};
list2 = {{f[x]}, {Framed[Cos[x]], f''[x]}};

i.e. apply a listable function in these lists that return the list with a function such as Framed[x] applied in whatever functions are not f and its derivatives.

$\endgroup$
10
  • 4
    $\begingroup$ Very respectfully, but a member for 11 years posting an image instead of properly formatted code is not good for newer members... $\endgroup$
    – bmf
    Feb 25, 2023 at 15:10
  • $\begingroup$ @bmf I was going to post the code, but it gets a kinda unreadable. With lots of [Prime], etc. $\endgroup$
    – Red Banana
    Feb 25, 2023 at 15:13
  • $\begingroup$ @bmf Done. $$$$ $\endgroup$
    – Red Banana
    Feb 25, 2023 at 15:14
  • $\begingroup$ I had already done it for you. it's the code about the image. but thanks for taking the time to edit :-) $\endgroup$
    – bmf
    Feb 25, 2023 at 15:15
  • 1
    $\begingroup$ To make it clear, could you edit your question and show by typing in what the output should be? This will be more clear for the readers I think than trying to just use words to describe the output. I do not know what Framed is in your comment above. $\endgroup$
    – Nasser
    Feb 25, 2023 at 15:39

6 Answers 6

8
$\begingroup$

Using Replace:

Replace[#
    , k : Except[f[__] | Derivative[_][f][_]] ->
     Framed[k
      , RoundingRadius -> 5
      , Background -> Yellow
      ], {2}
    ] & /@ {list1, list2} // Column

enter image description here

Now you can parse (read) this roughly as:

Replace from (level 2 only) of the following lists, the pattern k that consists of anything Except f[_] or any of its derivatives and Frame it with options.

$\endgroup$
1
  • $\begingroup$ (+1) Very nice, Indeed! $\endgroup$ Feb 25, 2023 at 18:11
8
$\begingroup$
h = Replace[#, b_?(FreeQ @ f) :> Highlighted[b], 2] &;


h @ list1 

enter image description here

h @ list2

enter image description here

$\endgroup$
3
  • $\begingroup$ How can I enhance it and apply it like: h = Replace[#, b_?(FreeQ[f]) :> Highlighted[b], All] so that it doesn't highlight the x's? $\endgroup$
    – Red Banana
    Feb 26, 2023 at 3:09
  • $\begingroup$ I guess I found a way: h = Replace[#, {x -> x, b_?(FreeQ[f]) :> Highlighted[b]}, All] &;. $\endgroup$
    – Red Banana
    Feb 26, 2023 at 3:11
  • $\begingroup$ The trouble is that it doesn't work if we get more complicated expressions such as list1 = {{f[x], Sin[x]}, {f'[x]}, {f''[x Cos[x]]}}. It will highlight the x Cos[x]. $\endgroup$
    – Red Banana
    Feb 26, 2023 at 3:17
7
$\begingroup$
$Version

(* "13.2.1 for Mac OS X ARM (64-bit) (January 27, 2023)" *)

ClearAll["Global`*"]

list1 = {{f[x], Sin[x]}, {f'[x]}, {f''[x]}, {Sin[x], Cos[x]}};
list2 = {{f[x]}, {Cos[x], f''[x]}};

notf[x_List] := If[FreeQ[#, f], Framed[#], #] & /@ x

notf /@ list1

enter image description here

notf /@ list2

enter image description here

SetAttributes[notf2, Listable];

notf2[x_] := If[FreeQ[x, f], Framed[x], x]

notf2@list1

enter image description here

notf2@list2

enter image description here

$\endgroup$
1
  • $\begingroup$ This is really nice, I didn't know FreeQ[]. I guess this will help me A LOT in future projects! $\endgroup$
    – Red Banana
    Feb 25, 2023 at 16:08
6
$\begingroup$

Another way to do this is as follows:

notf = Internal`CopyListStructure[#, 
If[SameQ[Variables[Level[Head[#], {-1}]], {f}], #, Framed[#]] /@Flatten@#] &;

Test:

notf@list1

enter image description here

notf@list2

enter image description here

Just to remember an undocumented function! :-)

$\endgroup$
6
$\begingroup$

Another way, thanks to the help I got from how-to-use-position-to-select-based-on-absence-of-a-pattern which is to find positions of entries that do not have the patttern f[_] or the pattern for derivative of any order and then use MapAt to put a frame around the positions found.

list1 = {{f[x], Sin[x]}, {f'[x]}, {f''[x]}, {Sin[x], Cos[x]}};
list2 = {{f[x]}, {Cos[x], f''[x]}};
pattern = (FreeQ[#, Alternatives[f[_], Derivative[_][f][_]]] &);
p1 = Position[list1, _?pattern, {2}, Heads -> False];

And now

MapAt[Framed[#] &, list1, p1]

Mathematica graphics

p2 = Position[list2, _?pattern, {2}, Heads -> False];
MapAt[Framed[#] &, list2, p2]

Mathematica graphics

$\endgroup$
5
$\begingroup$

Try this:

list1 = {{f[x], Sin[x]}, {f'[x]}, {f''[x]}};
list2 = {{f[x]}, {Cos[x], f''[x]}};

list1 /. f[x] -> Nothing /. Derivative[n_][f][x] -> Nothing // Flatten

(* {Sin[x]} *)


list2 /. f[x] -> Nothing /. Derivative[n_][f][x] -> Nothing // Flatten

(*   {Cos[x]}  *)

Have fun!

$\endgroup$
2
  • $\begingroup$ I'll need the f's and it's derivatives. I just want to replace whatever is not f and it's derivatives by Framed[something_not_f_and_its_derivatives]. $\endgroup$
    – Red Banana
    Feb 25, 2023 at 16:00
  • 1
    $\begingroup$ @Red Banan Look, that's opposite to what you initially wrote. $\endgroup$ Feb 25, 2023 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.