Is there a faster way to calculate Abs[z]^2 numerically?What are some useful, undocumented Mathematica functions?Is Abs[z]^2 a bad way to calculate the square modulus of z?Replacement of Do loopsIs there a faster way to create a matrix of indices from ragged data?Is Abs[z]^2 a bad way to calculate the square modulus of z?Faster way to perform SameQ[Reduce[…], Reduce[…]]Is there a faster way to Map an Association?Faster way to extract partial data from AdjacencyMatrixIs there a package that can calculate the Ricci tensor from a numerically given metric?Is there a good way to check, whether a small value produced numerically is a symbolic zero?Any faster way to compute this?faster way to merge dataIs there a better way to calculate the semivariance of a list?

Is there an academic word that means "to split hairs over"?

Why are goodwill impairments on the statement of cash-flows of GE?

Why did the soldiers of the North disobey Jon?

APFS - how do I enable transparent compression

Could there be something like aerobatic smoke trails in the vacuum of space?

Is the seat-belt sign activation when a pilot goes to the lavatory standard procedure?

My bread in my bread maker rises and then falls down just after cooking starts

How would you translate "grit" (personality trait) to Chinese?

How could it be that 80% of townspeople were farmers during the Edo period in Japan?

Could a space colony 1g from the sun work?

Capital gains on stocks sold to take initial investment off the table

Do United's 787-9 Dreamliners have personal HDMI ports?

Should I communicate in my applications that I'm unemployed out of choice rather than because nobody will have me?

Would life always name the light from their sun "white"

Network latencies between opposite ends of the Earth

Is my test coverage up to snuff?

God-Pharaoh's Statue and Finale Of Promise

Formal Definition of Dot Product

Why commonly or frequently used fonts sizes are even numbers like 10px, 12px, 16px, 24px, or 32px?

How to continually let my readers know what time it is in my story, in an organic way?

How to rename multiple files in a directory at the same time

In season 17 does LoN buff work against season journey set rewards?

Why would someone open a Netflix account using my Gmail address?

What dog breeds survive the apocalypse for generations?



Is there a faster way to calculate Abs[z]^2 numerically?


What are some useful, undocumented Mathematica functions?Is Abs[z]^2 a bad way to calculate the square modulus of z?Replacement of Do loopsIs there a faster way to create a matrix of indices from ragged data?Is Abs[z]^2 a bad way to calculate the square modulus of z?Faster way to perform SameQ[Reduce[…], Reduce[…]]Is there a faster way to Map an Association?Faster way to extract partial data from AdjacencyMatrixIs there a package that can calculate the Ricci tensor from a numerically given metric?Is there a good way to check, whether a small value produced numerically is a symbolic zero?Any faster way to compute this?faster way to merge dataIs there a better way to calculate the semivariance of a list?













11












$begingroup$


Here I'm not interested in accuracy (see 13614) but rather in raw speed. You'd think that for a complex machine-precision number z, calculating Abs[z]^2 should be faster than calculating Abs[z] because the latter requires a square root whereas the former does not. Yet it's not so:



s = RandomVariate[NormalDistribution[], 10^7, 2].1, I;
Developer`PackedArrayQ[s]
(* True *)
Abs[s]^2; // AbsoluteTiming // First
(* 0.083337 *)
Abs[s]; // AbsoluteTiming // First
(* 0.033179 *)


This indicates that Abs[z]^2 is really calculated by summing the squares of real and imaginary parts, taking a square root (for Abs[z]), and then re-squaring (for Abs[z]^2).



Is there a faster way to compute Abs[z]^2? Is there a hidden equivalent to the GSL's gsl_complex_abs2 function? The source code of this GSL function is simply to return Re[z]^2+Im[z]^2; no fancy tricks.










share|improve this question











$endgroup$







  • 1




    $begingroup$
    Here's an even slower way: (Re[#]^2 + Im[#]^2) & /@ s. And even slower still: Total[ReIm[#]^2] & /@ s
    $endgroup$
    – bill s
    May 10 at 14:24
















11












$begingroup$


Here I'm not interested in accuracy (see 13614) but rather in raw speed. You'd think that for a complex machine-precision number z, calculating Abs[z]^2 should be faster than calculating Abs[z] because the latter requires a square root whereas the former does not. Yet it's not so:



s = RandomVariate[NormalDistribution[], 10^7, 2].1, I;
Developer`PackedArrayQ[s]
(* True *)
Abs[s]^2; // AbsoluteTiming // First
(* 0.083337 *)
Abs[s]; // AbsoluteTiming // First
(* 0.033179 *)


This indicates that Abs[z]^2 is really calculated by summing the squares of real and imaginary parts, taking a square root (for Abs[z]), and then re-squaring (for Abs[z]^2).



Is there a faster way to compute Abs[z]^2? Is there a hidden equivalent to the GSL's gsl_complex_abs2 function? The source code of this GSL function is simply to return Re[z]^2+Im[z]^2; no fancy tricks.










share|improve this question











$endgroup$







  • 1




    $begingroup$
    Here's an even slower way: (Re[#]^2 + Im[#]^2) & /@ s. And even slower still: Total[ReIm[#]^2] & /@ s
    $endgroup$
    – bill s
    May 10 at 14:24














11












11








11


2



$begingroup$


Here I'm not interested in accuracy (see 13614) but rather in raw speed. You'd think that for a complex machine-precision number z, calculating Abs[z]^2 should be faster than calculating Abs[z] because the latter requires a square root whereas the former does not. Yet it's not so:



s = RandomVariate[NormalDistribution[], 10^7, 2].1, I;
Developer`PackedArrayQ[s]
(* True *)
Abs[s]^2; // AbsoluteTiming // First
(* 0.083337 *)
Abs[s]; // AbsoluteTiming // First
(* 0.033179 *)


This indicates that Abs[z]^2 is really calculated by summing the squares of real and imaginary parts, taking a square root (for Abs[z]), and then re-squaring (for Abs[z]^2).



Is there a faster way to compute Abs[z]^2? Is there a hidden equivalent to the GSL's gsl_complex_abs2 function? The source code of this GSL function is simply to return Re[z]^2+Im[z]^2; no fancy tricks.










share|improve this question











$endgroup$




Here I'm not interested in accuracy (see 13614) but rather in raw speed. You'd think that for a complex machine-precision number z, calculating Abs[z]^2 should be faster than calculating Abs[z] because the latter requires a square root whereas the former does not. Yet it's not so:



s = RandomVariate[NormalDistribution[], 10^7, 2].1, I;
Developer`PackedArrayQ[s]
(* True *)
Abs[s]^2; // AbsoluteTiming // First
(* 0.083337 *)
Abs[s]; // AbsoluteTiming // First
(* 0.033179 *)


This indicates that Abs[z]^2 is really calculated by summing the squares of real and imaginary parts, taking a square root (for Abs[z]), and then re-squaring (for Abs[z]^2).



Is there a faster way to compute Abs[z]^2? Is there a hidden equivalent to the GSL's gsl_complex_abs2 function? The source code of this GSL function is simply to return Re[z]^2+Im[z]^2; no fancy tricks.







performance-tuning numerics






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 10 at 14:23







Roman

















asked May 10 at 14:17









RomanRoman

8,21811238




8,21811238







  • 1




    $begingroup$
    Here's an even slower way: (Re[#]^2 + Im[#]^2) & /@ s. And even slower still: Total[ReIm[#]^2] & /@ s
    $endgroup$
    – bill s
    May 10 at 14:24













  • 1




    $begingroup$
    Here's an even slower way: (Re[#]^2 + Im[#]^2) & /@ s. And even slower still: Total[ReIm[#]^2] & /@ s
    $endgroup$
    – bill s
    May 10 at 14:24








1




1




$begingroup$
Here's an even slower way: (Re[#]^2 + Im[#]^2) & /@ s. And even slower still: Total[ReIm[#]^2] & /@ s
$endgroup$
– bill s
May 10 at 14:24





$begingroup$
Here's an even slower way: (Re[#]^2 + Im[#]^2) & /@ s. And even slower still: Total[ReIm[#]^2] & /@ s
$endgroup$
– bill s
May 10 at 14:24











1 Answer
1






active

oldest

votes


















17












$begingroup$

There's Internal`AbsSquare:



s = RandomVariate[NormalDistribution[], 10^7, 2].1, I;
foo = Internal`AbsSquare[s]; // AbsoluteTiming // First
murf = Abs[s]^2; // AbsoluteTiming // First
(*
0.022909
0.063441
*)

foo == murf
(* True *)





share|improve this answer









$endgroup$








  • 1




    $begingroup$
    Ah yes precisely what I was looking for, many thanks Michael! Is there a repository of such tricks?
    $endgroup$
    – Roman
    May 10 at 14:25






  • 1




    $begingroup$
    @Roman I was just looking. I thought there was a post about useful Internal` functions, but I couldn't find it just now. The context contains some useful numerical functions like Log1p and Expm1. Statistics`Library` also contains some nice, well-programmed functions.
    $endgroup$
    – Michael E2
    May 10 at 14:31






  • 2




    $begingroup$
    @MichaelE2 mathematica.stackexchange.com/questions/805/…
    $endgroup$
    – Chris K
    May 10 at 14:31







  • 1




    $begingroup$
    @ChrisK That must be it! Thanks.
    $endgroup$
    – Michael E2
    May 10 at 14:32






  • 1




    $begingroup$
    @CATrevillian I would have thought it was in the MKL (Intel Math Kernel Library), but I didn't find it there. I guess I don't know.
    $endgroup$
    – Michael E2
    May 11 at 3:10











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f198120%2fis-there-a-faster-way-to-calculate-absz2-numerically%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









17












$begingroup$

There's Internal`AbsSquare:



s = RandomVariate[NormalDistribution[], 10^7, 2].1, I;
foo = Internal`AbsSquare[s]; // AbsoluteTiming // First
murf = Abs[s]^2; // AbsoluteTiming // First
(*
0.022909
0.063441
*)

foo == murf
(* True *)





share|improve this answer









$endgroup$








  • 1




    $begingroup$
    Ah yes precisely what I was looking for, many thanks Michael! Is there a repository of such tricks?
    $endgroup$
    – Roman
    May 10 at 14:25






  • 1




    $begingroup$
    @Roman I was just looking. I thought there was a post about useful Internal` functions, but I couldn't find it just now. The context contains some useful numerical functions like Log1p and Expm1. Statistics`Library` also contains some nice, well-programmed functions.
    $endgroup$
    – Michael E2
    May 10 at 14:31






  • 2




    $begingroup$
    @MichaelE2 mathematica.stackexchange.com/questions/805/…
    $endgroup$
    – Chris K
    May 10 at 14:31







  • 1




    $begingroup$
    @ChrisK That must be it! Thanks.
    $endgroup$
    – Michael E2
    May 10 at 14:32






  • 1




    $begingroup$
    @CATrevillian I would have thought it was in the MKL (Intel Math Kernel Library), but I didn't find it there. I guess I don't know.
    $endgroup$
    – Michael E2
    May 11 at 3:10















17












$begingroup$

There's Internal`AbsSquare:



s = RandomVariate[NormalDistribution[], 10^7, 2].1, I;
foo = Internal`AbsSquare[s]; // AbsoluteTiming // First
murf = Abs[s]^2; // AbsoluteTiming // First
(*
0.022909
0.063441
*)

foo == murf
(* True *)





share|improve this answer









$endgroup$








  • 1




    $begingroup$
    Ah yes precisely what I was looking for, many thanks Michael! Is there a repository of such tricks?
    $endgroup$
    – Roman
    May 10 at 14:25






  • 1




    $begingroup$
    @Roman I was just looking. I thought there was a post about useful Internal` functions, but I couldn't find it just now. The context contains some useful numerical functions like Log1p and Expm1. Statistics`Library` also contains some nice, well-programmed functions.
    $endgroup$
    – Michael E2
    May 10 at 14:31






  • 2




    $begingroup$
    @MichaelE2 mathematica.stackexchange.com/questions/805/…
    $endgroup$
    – Chris K
    May 10 at 14:31







  • 1




    $begingroup$
    @ChrisK That must be it! Thanks.
    $endgroup$
    – Michael E2
    May 10 at 14:32






  • 1




    $begingroup$
    @CATrevillian I would have thought it was in the MKL (Intel Math Kernel Library), but I didn't find it there. I guess I don't know.
    $endgroup$
    – Michael E2
    May 11 at 3:10













17












17








17





$begingroup$

There's Internal`AbsSquare:



s = RandomVariate[NormalDistribution[], 10^7, 2].1, I;
foo = Internal`AbsSquare[s]; // AbsoluteTiming // First
murf = Abs[s]^2; // AbsoluteTiming // First
(*
0.022909
0.063441
*)

foo == murf
(* True *)





share|improve this answer









$endgroup$



There's Internal`AbsSquare:



s = RandomVariate[NormalDistribution[], 10^7, 2].1, I;
foo = Internal`AbsSquare[s]; // AbsoluteTiming // First
murf = Abs[s]^2; // AbsoluteTiming // First
(*
0.022909
0.063441
*)

foo == murf
(* True *)






share|improve this answer












share|improve this answer



share|improve this answer










answered May 10 at 14:24









Michael E2Michael E2

152k12208491




152k12208491







  • 1




    $begingroup$
    Ah yes precisely what I was looking for, many thanks Michael! Is there a repository of such tricks?
    $endgroup$
    – Roman
    May 10 at 14:25






  • 1




    $begingroup$
    @Roman I was just looking. I thought there was a post about useful Internal` functions, but I couldn't find it just now. The context contains some useful numerical functions like Log1p and Expm1. Statistics`Library` also contains some nice, well-programmed functions.
    $endgroup$
    – Michael E2
    May 10 at 14:31






  • 2




    $begingroup$
    @MichaelE2 mathematica.stackexchange.com/questions/805/…
    $endgroup$
    – Chris K
    May 10 at 14:31







  • 1




    $begingroup$
    @ChrisK That must be it! Thanks.
    $endgroup$
    – Michael E2
    May 10 at 14:32






  • 1




    $begingroup$
    @CATrevillian I would have thought it was in the MKL (Intel Math Kernel Library), but I didn't find it there. I guess I don't know.
    $endgroup$
    – Michael E2
    May 11 at 3:10












  • 1




    $begingroup$
    Ah yes precisely what I was looking for, many thanks Michael! Is there a repository of such tricks?
    $endgroup$
    – Roman
    May 10 at 14:25






  • 1




    $begingroup$
    @Roman I was just looking. I thought there was a post about useful Internal` functions, but I couldn't find it just now. The context contains some useful numerical functions like Log1p and Expm1. Statistics`Library` also contains some nice, well-programmed functions.
    $endgroup$
    – Michael E2
    May 10 at 14:31






  • 2




    $begingroup$
    @MichaelE2 mathematica.stackexchange.com/questions/805/…
    $endgroup$
    – Chris K
    May 10 at 14:31







  • 1




    $begingroup$
    @ChrisK That must be it! Thanks.
    $endgroup$
    – Michael E2
    May 10 at 14:32






  • 1




    $begingroup$
    @CATrevillian I would have thought it was in the MKL (Intel Math Kernel Library), but I didn't find it there. I guess I don't know.
    $endgroup$
    – Michael E2
    May 11 at 3:10







1




1




$begingroup$
Ah yes precisely what I was looking for, many thanks Michael! Is there a repository of such tricks?
$endgroup$
– Roman
May 10 at 14:25




$begingroup$
Ah yes precisely what I was looking for, many thanks Michael! Is there a repository of such tricks?
$endgroup$
– Roman
May 10 at 14:25




1




1




$begingroup$
@Roman I was just looking. I thought there was a post about useful Internal` functions, but I couldn't find it just now. The context contains some useful numerical functions like Log1p and Expm1. Statistics`Library` also contains some nice, well-programmed functions.
$endgroup$
– Michael E2
May 10 at 14:31




$begingroup$
@Roman I was just looking. I thought there was a post about useful Internal` functions, but I couldn't find it just now. The context contains some useful numerical functions like Log1p and Expm1. Statistics`Library` also contains some nice, well-programmed functions.
$endgroup$
– Michael E2
May 10 at 14:31




2




2




$begingroup$
@MichaelE2 mathematica.stackexchange.com/questions/805/…
$endgroup$
– Chris K
May 10 at 14:31





$begingroup$
@MichaelE2 mathematica.stackexchange.com/questions/805/…
$endgroup$
– Chris K
May 10 at 14:31





1




1




$begingroup$
@ChrisK That must be it! Thanks.
$endgroup$
– Michael E2
May 10 at 14:32




$begingroup$
@ChrisK That must be it! Thanks.
$endgroup$
– Michael E2
May 10 at 14:32




1




1




$begingroup$
@CATrevillian I would have thought it was in the MKL (Intel Math Kernel Library), but I didn't find it there. I guess I don't know.
$endgroup$
– Michael E2
May 11 at 3:10




$begingroup$
@CATrevillian I would have thought it was in the MKL (Intel Math Kernel Library), but I didn't find it there. I guess I don't know.
$endgroup$
– Michael E2
May 11 at 3:10

















draft saved

draft discarded
















































Thanks for contributing an answer to Mathematica Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f198120%2fis-there-a-faster-way-to-calculate-absz2-numerically%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form