5
$\begingroup$

This works

Through[{#*2 &, #*8 &}[a]]

{2 a, 8 a}

But if I want to generalize to a 2 dimensional array of functions like this

Through[{{#*2 &, #*8 &}, {#*3 &, #*5 &}}[a]]

It doesn't work. I've tried to add a third argument to Through such as 1 or 2 or {1,1} and this doesn't help. To be clear, I know how to work around this, but I just don't understand why it isn't easy to generalize through to a general array of functions.

$\endgroup$
7
  • 3
    $\begingroup$ Working around it: Through[#[a]] & /@ {{#*2 &, #*8 &}, {#*3 &, #*5 &}} $\endgroup$
    – Syed
    Commented May 2, 2023 at 15:58
  • 2
    $\begingroup$ {{#*2, #*8}, {#*3, #*5}} &[a] ? In other words, apply one function with the required structure? $\endgroup$
    – MarcoB
    Commented May 2, 2023 at 16:08
  • 1
    $\begingroup$ Thank for the work arounds. That is currently what I have in my code. However, I'm just frustrated that Through doesn't have an obvious extension to arrays greater than one dimension. $\endgroup$
    – Chris
    Commented May 2, 2023 at 17:04
  • 3
    $\begingroup$ Through doesn't assume that the head of the head is List. And it certainly doesn't assume that the head is a full matrix. So, there would need to be some way to recursively find the same head-head symbol at all levels of the head structure and then figure out how it's supposed to be applied. That seems very complicated and not as semantically clear as the current Through definition. Probably better to go with one of the Map- or Apply-related functions. $\endgroup$
    – lericr
    Commented May 2, 2023 at 17:23
  • 1
    $\begingroup$ It is unfortunate this question is closed because several creative approaches to the problem appeared over the last few hours. $\endgroup$
    – Chris
    Commented May 3, 2023 at 15:35

5 Answers 5

5
$\begingroup$
Through/@Through[{{#*2 &, #*8 &}, {#*3 &, #*5 &}}[a]]

(*{{2*a, 8*a}, {3*a, 5*a}} *)

(2)

Through[{{f, g},{x,y}}[a]]
(* {{f, g}[a], {x, y}[a]} *)

and:

Through/@Through[{{f, g},{x,y}}[a]]

(* {{f[a], g[a]}, {x[a], y[a]}} *)
$\endgroup$
4
$\begingroup$

I propose :

Map[#[a] &, {{#*2 &, #*8 &}, {#*3 &, #*5 &}}, {2}]  

{{2 a, 8 a}, {3 a, 5 a}}

Though I have never used it.

$\endgroup$
1
  • $\begingroup$ My objection to this workout around is that it isn't readable, in the sense that I can't just look at it and red it like an english sentence. $\endgroup$
    – Chris
    Commented May 2, 2023 at 19:54
4
$\begingroup$

Using Query

Query[{{#*2 &, #*8 &}, {#*3 &, #*5 &}}] @ a

{{2 a, 8 a}, {3 a, 5 a}}

Using Comap (new in 14.0)

Comap[{{#*2 &, #*8 &}, {#*3 &, #*5 &}}, a, {2}]

{{2 a, 8 a}, {3 a, 5 a}}

$\endgroup$
1
  • $\begingroup$ Query is good since it work for the general cases. $\endgroup$
    – cvgmt
    Commented May 9 at 0:39
3
$\begingroup$

I learn this from someone.

Clear[operator];
operator[head_] := head[a];
SetAttributes[operator, Listable];
operator[{{#*2 &, #*8 &}, {#*3 &, #*5 &}, {# &, # &, # &}, # &}]

{{2 a, 8 a}, {3 a, 5 a}, {a, a, a}, a}.

$\endgroup$
2
$\begingroup$

Using Thread:

Apply[#1@#2&,Thread/@Thread[{{{#*2 &, #*8 &}, {#*3 &, #*5 &}},a}],{2}]

{{2 a,8 a},{3 a,5 a}}

$\endgroup$

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.