Dot - mystery consumption time
- To: mathgroup at smc.vnet.net
- Subject: [mg44012] Dot - mystery consumption time
- From: kujanp at fel.cvut.cz (Petr Kujan)
- Date: Fri, 17 Oct 2003 05:14:42 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Hello MathGroup, I would be grateful if someone could explain the difference in consumption time. I redefine function Dot[]. Here is a Mathematica document with comments. Thank in advance for all your help. Best regards, Petr. (************** Content-type: application/mathematica ************** CreatedBy='Mathematica 5.0' Mathematica-Compatible Notebook This notebook can be used with any Mathematica-compatible application, such as Mathematica, MathReader or Publicon. The data for the notebook starts with the line containing stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info at wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. *******************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 11298, 323]*) (*NotebookOutlinePosition[ 11943, 345]*) (* CellTagsIndexPosition[ 11899, 341]*) (*WindowFrame->Normal*) Notebook[{ Cell["\<\ I use Mathematica 5.0 -> SparseArray[ ] for fast generating zero matrix in \ function sm[]. PC with Duron 750GHz, 256MB, Win2k \ \>", "Text"], Cell["Auxiliary function", "Text"], Cell[BoxData[{ \(\(\(prand[deg_, rowcol_, var_] := pm[Table[Random[Real, {\(-2\), 2}], Evaluate[{deg + 1}, {rowcol}, {rowcol}]] // ToPackedArray, var, deg];\)\(\[IndentingNewLine]\) \)\), "\[IndentingNewLine]", \(\(\(size[pol : pm[pmc_, var_, deg_]] := Dimensions[pmc[\([1]\)]];\)\(\[IndentingNewLine]\) \)\), "\[IndentingNewLine]", \(\(\(droplzer[dat_] := Module[{lendat = Length[dat], i}, i = lendat; \[IndentingNewLine]While[ And @@ \(ZeroQ /@ \(Hold @@ Flatten[dat[\([i]\)]]\)\), \(i = i - 1;\)]; \[IndentingNewLine]Drop[ dat, \(-\((lendat - i)\)\)]];\)\(\[IndentingNewLine]\) \)\), "\[IndentingNewLine]", \(\(prepmat[r_Integer, c_Integer, ini : \(\((_?NumericQ)\) : 0. \)] /; \((r > 0 && c > 0)\) := If[$VersionNumber < 5, Table[ini, {r}, {c}], Normal[SparseArray[{1, 1} -> ini, {r, c}, ini]]];\)\[IndentingNewLine]\), "\[IndentingNewLine]", \(\(sm[A : pm[pmcA_, var_, deg_], col_?Positive] := Module[{m, n, mat, flatpmcA, prepnum}, {m, n} = size[A]; \[IndentingNewLine]flatpmcA = Flatten[pmcA, 1]; \[IndentingNewLine]mat = prepmat[m*\((col + deg)\), col*n, 0. ]; \[IndentingNewLine]Do[ mat[\([Range[\((i - 1)\)*m + 1, \((i + deg)\)*m], Range[\((i - 1)\)*n + 1, i*n]]\)] = flatpmcA, {i, col}]; \[IndentingNewLine]mat];\)\)}], "Input"], Cell["Redefine standard function Dot[] for my object pm[]", "Text"], Cell["\<\ Fast computation - OK !! now computation Chop[ ] and droplzer[ ] is commentary\ \>", "Text"], Cell[BoxData[ \(pm /: Dot[A : pm[pmcA_, varA_, degA_], B : pm[pmcB_, varB_, degB_]] := \[IndentingNewLine]Module[{me, zer, pmc, pardot, cho, s1, s2}, \[IndentingNewLine] (*zeroing\ data*) \[IndentingNewLine] (*me \ = NonZeroMin[{pmcA, pmcB}]; (*\(! \(! slow\)\)*) *) \[IndentingNewLine]me = 0. ; \[IndentingNewLine]zer = 10. ^\(-10\); \[IndentingNewLine]Print["\<---------\>"]; \ \[IndentingNewLine]Print["\<s1: \>", Timing[\[IndentingNewLine]\(s1 = sm[A, degB + 1];\)\[IndentingNewLine]]]; \ \[IndentingNewLine]Print["\<s2: \>", Timing[\[IndentingNewLine]\(s2 = sm[B, 1];\)\[IndentingNewLine]]]; \ \[IndentingNewLine]Print["\<pardot: \>", Timing[\[IndentingNewLine]\(pardot = Partition[ s1 . s2, \(size[ A]\)[\([1]\)]];\)\[IndentingNewLine]]]; \ \[IndentingNewLine] (*\ \(\(**\)\(**\)\(**\)\(**\)\(**\)\(**\)\(**\)\(\ \[IndentingNewLine]\)\(Print["\<chop: \>", Timing[\[IndentingNewLine]\(cho = Chop[par, me*zer];\)\[IndentingNewLine]]]\)\); \ \[IndentingNewLine]\(Print["\<droplzer\>", Timing[\[IndentingNewLine]\(pmc = droplzer[ cho];\)\[IndentingNewLine]]];\[IndentingNewLine] ** ** \ ** ** ** ** ** \)\ *) \[IndentingNewLine]pm[pardot, varA, Length[pardot] - 1]\[IndentingNewLine]]\)], "Input"], Cell[CellGroupData[{ Cell[BoxData[{ \(SeedRandom[1]\), "\[IndentingNewLine]", \(\(a = prand[20, 50, s];\)\), "\[IndentingNewLine]", \(\(a . a . a;\) // Timing\)}], "Input"], Cell[BoxData[ \("---------"\)], "Print"], Cell[BoxData[ InterpretationBox[\("s1: "\[InvisibleSpace]{0.3309999999999995`\ Second, Null}\), SequenceForm[ "s1: ", { Times[ .33099999999999952, Second], Null}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("s2: "\[InvisibleSpace]{0.009999999999999787`\ \ Second, Null}\), SequenceForm[ "s2: ", { Times[ .0099999999999997868, Second], Null}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("pardot: "\[InvisibleSpace]{0.45099999999999874`\ \ Second, Null}\), SequenceForm[ "pardot: ", { Times[ .45099999999999874, Second], Null}], Editable->False]], "Print"], Cell[BoxData[ \("---------"\)], "Print"], Cell[BoxData[ InterpretationBox[\("s1: "\[InvisibleSpace]{0.370000000000001`\ Second, Null}\), SequenceForm[ "s1: ", { Times[ .37000000000000099, Second], Null}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("s2: "\[InvisibleSpace]{0.009999999999999787`\ \ Second, Null}\), SequenceForm[ "s2: ", { Times[ .0099999999999997868, Second], Null}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("pardot: "\[InvisibleSpace]{0.6310000000000002`\ \ Second, Null}\), SequenceForm[ "pardot: ", { Times[ .63100000000000023, Second], Null}], Editable->False]], "Print"], Cell[BoxData[ \({1.8230000000000004`\ Second, Null}\)], "Output"] }, Open ]], Cell["\<\ small consumption time for computation s1=sm[..]: 0.37 s2=sm[..]: 0.01 pardot: 0.631\ \>", "Text"], Cell["\<\ !! now only uncommentary last functions Chop[] and droplzer[] and consumption time for computation s1, s2 and pardot is larger !! s1=sm[..]: 0.37 -> 0.64 s2=sm[..]: 0.01 -> 0.02 pardot: 0.631 -> 1.112 I expect time increasing only 0.14 second Chop[..]: 0.14 droplzer: 0\ \>", "Text"], Cell[BoxData[{ \(\(Clear[pm];\)\), "\[IndentingNewLine]", \(pm /: Dot[A : pm[pmcA_, varA_, degA_], B : pm[pmcB_, varB_, degB_]] := \[IndentingNewLine]Module[{me, zer, pmc, pardot, cho, s1, s2}, \[IndentingNewLine] (*zeroing\ data*) \[IndentingNewLine] (*me \ = NonZeroMin[{pmcA, pmcB}]; (*\(! \(! slow\)\)*) *) \[IndentingNewLine]me = 0. ; \[IndentingNewLine]zer = 10. ^\(-10\); \[IndentingNewLine]Print["\<---------\>"]; \ \[IndentingNewLine]Print["\<s1: \>", Timing[\[IndentingNewLine]\(s1 = sm[A, degB + 1];\)\[IndentingNewLine]]]; \ \[IndentingNewLine]Print["\<s2: \>", Timing[\[IndentingNewLine]\(s2 = sm[B, 1];\)\[IndentingNewLine]]]; \ \[IndentingNewLine]Print["\<dot: \>", Timing[\[IndentingNewLine]\(pardot = Partition[ s1 . s2, \(size[ A]\)[\([1]\)]];\)\[IndentingNewLine]]]; \ \[IndentingNewLine]Print["\<chop: \>", Timing[\[IndentingNewLine]\(cho = Chop[pardot, me*zer];\)\[IndentingNewLine]]]; \[IndentingNewLine]Print["\ \<droplzer\>", Timing[\[IndentingNewLine]\(pmc = droplzer[cho];\)\[IndentingNewLine]]]; \[IndentingNewLine]pm[ pmc, varA, Length[pmc] - 1]\[IndentingNewLine]]\)}], "Input"], Cell["Slow computation", "Text"], Cell[CellGroupData[{ Cell[BoxData[{ \(SeedRandom[1]\), "\[IndentingNewLine]", \(\(a = prand[20, 50, s];\)\), "\[IndentingNewLine]", \(\(a . a . a;\) // Timing\)}], "Input"], Cell[BoxData[ \("---------"\)], "Print"], Cell[BoxData[ InterpretationBox[\("s1: "\[InvisibleSpace]{0.3200000000000003`\ Second, Null}\), SequenceForm[ "s1: ", { Times[ .32000000000000028, Second], Null}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("s2: "\[InvisibleSpace]{0.009999999999999787`\ \ Second, Null}\), SequenceForm[ "s2: ", { Times[ .0099999999999997868, Second], Null}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("dot: "\[InvisibleSpace]{0.42100000000000115`\ \ Second, Null}\), SequenceForm[ "dot: ", { Times[ .42100000000000115, Second], Null}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("chop: "\[InvisibleSpace]{0.08999999999999986`\ \ Second, Null}\), SequenceForm[ "chop: ", { Times[ .089999999999999858, Second], Null}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("droplzer"\[InvisibleSpace]{0.`\ Second, Null}\), SequenceForm[ "droplzer", { Times[ 0.0, Second], Null}], Editable->False]], "Print"], Cell[BoxData[ \("---------"\)], "Print"], Cell[BoxData[ InterpretationBox[\("s1: "\[InvisibleSpace]{0.641`\ Second, Null}\), SequenceForm[ "s1: ", { Times[ .64100000000000001, Second], Null}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("s2: "\[InvisibleSpace]{0.019999999999999574`\ \ Second, Null}\), SequenceForm[ "s2: ", { Times[ .019999999999999574, Second], Null}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("dot: "\[InvisibleSpace]{1.112`\ Second, Null}\), SequenceForm[ "dot: ", { Times[ 1.1120000000000001, Second], Null}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("chop: "\[InvisibleSpace]{0.14000000000000057`\ \ Second, Null}\), SequenceForm[ "chop: ", { Times[ .14000000000000057, Second], Null}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("droplzer"\[InvisibleSpace]{0.`\ Second, Null}\), SequenceForm[ "droplzer", { Times[ 0.0, Second], Null}], Editable->False]], "Print"], Cell[BoxData[ \({3.024000000000001`\ Second, Null}\)], "Output"] }, Open ]] }, FrontEndVersion->"5.0 for Microsoft Windows", ScreenRectangle->{{0, 1280}, {0, 951}}, WindowSize->{853, 740}, WindowMargins->{{51, Automatic}, {Automatic, 57}} ] (******************************************************************* Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. *******************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[1754, 51, 154, 4, 52, "Text"], Cell[1911, 57, 34, 0, 33, "Text"], Cell[1948, 59, 1522, 28, 370, "Input"], Cell[3473, 89, 67, 0, 33, "Text"], Cell[3543, 91, 102, 3, 52, "Text"], Cell[3648, 96, 1543, 31, 530, "Input"], Cell[CellGroupData[{ Cell[5216, 131, 165, 3, 70, "Input"], Cell[5384, 136, 44, 1, 25, "Print"], Cell[5431, 139, 226, 5, 25, "Print"], Cell[5660, 146, 221, 5, 25, "Print"], Cell[5884, 153, 226, 5, 25, "Print"], Cell[6113, 160, 44, 1, 25, "Print"], Cell[6160, 163, 225, 5, 25, "Print"], Cell[6388, 170, 221, 5, 25, "Print"], Cell[6612, 177, 225, 5, 25, "Print"], Cell[6840, 184, 69, 1, 29, "Output"] }, Open ]], Cell[6924, 188, 109, 5, 90, "Text"], Cell[7036, 195, 294, 10, 185, "Text"], Cell[7333, 207, 1407, 29, 510, "Input"], Cell[8743, 238, 32, 0, 33, "Text"], Cell[CellGroupData[{ Cell[8800, 242, 165, 3, 70, "Input"], Cell[8968, 247, 44, 1, 25, "Print"], Cell[9015, 250, 226, 5, 25, "Print"], Cell[9244, 257, 221, 5, 25, "Print"], Cell[9468, 264, 220, 5, 25, "Print"], Cell[9691, 271, 223, 5, 25, "Print"], Cell[9917, 278, 192, 4, 25, "Print"], Cell[10112, 284, 44, 1, 25, "Print"], Cell[10159, 287, 202, 4, 25, "Print"], Cell[10364, 293, 220, 5, 25, "Print"], Cell[10587, 300, 204, 4, 25, "Print"], Cell[10794, 306, 222, 5, 25, "Print"], Cell[11019, 313, 192, 4, 25, "Print"], Cell[11214, 319, 68, 1, 29, "Output"] }, Open ]] } ] *) (******************************************************************* End of Mathematica Notebook file. *******************************************************************)