(* Content-type: application/vnd.wolfram.mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 8.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 157, 7] NotebookDataLength[ 742834, 14672] NotebookOptionsPosition[ 732641, 14506] NotebookOutlinePosition[ 733086, 14523] CellTagsIndexPosition[ 733043, 14520] WindowFrame->Normal*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell[TextData[StyleBox["Spherical Clustering: WKMeans and Incremental", FontSize->24, FontColor->RGBColor[0, 0, 1]]], "Section", CellChangeTimes->{{3.5834653677240553`*^9, 3.5834654173733435`*^9}, { 3.584592599574686*^9, 3.584592606361698*^9}, {3.5853840833321753`*^9, 3.5853841277116537`*^9}, {3.5854094556522703`*^9, 3.5854094625494823`*^9}, {3.5856368195312567`*^9, 3.5856368382396894`*^9}, {3.5858298722484055`*^9, 3.5858298725448055`*^9}, {3.5864063632009335`*^9, 3.5864064033166046`*^9}, {3.5864064559160995`*^9, 3.586406647972839*^9}, { 3.58640671107135*^9, 3.586406715877159*^9}, {3.5865659404883413`*^9, 3.5865659478213544`*^9}, {3.586566999964803*^9, 3.586567104536188*^9}, { 3.5865672242601995`*^9, 3.586567307752947*^9}, {3.5865692634118156`*^9, 3.586569295552872*^9}, {3.586569330082733*^9, 3.5865693858326316`*^9}, { 3.5865694634119687`*^9, 3.5865694927138205`*^9}, {3.5865698938861294`*^9, 3.5865699236725826`*^9}, {3.5866121586124725`*^9, 3.5866121743257003`*^9}, {3.5866498010394135`*^9, 3.5866498672277308`*^9}, {3.58665309774765*^9, 3.5866531016018567`*^9}, { 3.586662349848068*^9, 3.586662380531723*^9}, {3.586674934640195*^9, 3.5866750871706676`*^9}, {3.5873747801102047`*^9, 3.587374780562606*^9}, { 3.587374820683277*^9, 3.5873748217138786`*^9}, {3.5921860661462336`*^9, 3.592186155553369*^9}, {3.5921868106153545`*^9, 3.5921868225943727`*^9}, 3.592263157216728*^9, {3.5925373387709875`*^9, 3.592537347033*^9}, 3.592537391988068*^9, {3.5925378114897056`*^9, 3.592537886320818*^9}, { 3.5942677103371067`*^9, 3.594267715929115*^9}, {3.6021622777332*^9, 3.6021622782792015`*^9}, {3.602166511267437*^9, 3.602166560735124*^9}, { 3.6492086233993254`*^9, 3.6492086271433315`*^9}, {3.6492086676254025`*^9, 3.6492087287775097`*^9}, {3.6492185103650513`*^9, 3.6492185307386866`*^9}, {3.7335406342547283`*^9, 3.7335406455215335`*^9}, {3.7335407793795023`*^9, 3.733540842667445*^9}, { 3.807410856918437*^9, 3.807410908145829*^9}, {3.8108657982481346`*^9, 3.810865833639106*^9}, {3.8222235188016143`*^9, 3.8222235196320043`*^9}}, FontColor->RGBColor[ 1, 0, 0],ExpressionUUID->"29325eab-b148-47a7-9f90-3d4c11f36b13"], Cell[BoxData[ RowBox[{"(*", " ", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{ "Spherical", " ", "Clustering", " ", "is", " ", "a", " ", "complete", " ", "Mathematica"}], "-", RowBox[{ "program", " ", "for", " ", "spherical", "\n", "clustering", " ", "using", " ", "the", " ", "LS", " ", "distance"}], "-", RowBox[{"like", " ", RowBox[{"function", ".", "All"}], " ", "modules", " ", "needed", " ", "are", " ", "listed", "\n", "in", " ", "the", " ", RowBox[{"preamble", ".", "One"}], " ", "has", " ", "to", " ", "load", " ", "one", " ", "of", " ", "the", " ", "suggested", " ", "examples", " ", "first"}]}], ",", RowBox[{ "which", "\n", "will", " ", "automatically", " ", "perform", " ", "the", " ", RowBox[{"WKMeans", "[", "]"}], " ", RowBox[{"algorithm", ".", "Then"}], " ", "one", " ", "can", " ", "call", "\n", "the", " ", "incremental", " ", "algorithm", " ", RowBox[{"Inc", "[", "]"}], " ", "which", " ", "will"}], ",", RowBox[{ "among", " ", "partitions", " ", "obtained", " ", "and", "\n", "based", " ", "on", " ", "several", " ", "indexes"}], ",", RowBox[{"propose", " ", "the", " ", RowBox[{"MAPart", ".", "If"}], " ", "the", " ", "original", " ", "partition", " ", "is", "\n", "known"}], ",", RowBox[{ "it", " ", "will", " ", "also", " ", "calculate", " ", "the", " ", "Rand", " ", "and", " ", "Jaccard", " ", "indexes"}], ",", RowBox[{ "as", " ", "well", " ", "as", " ", "the", "\n", "Hausdorff", " ", "distance", " ", "between", " ", "sets", " ", "of", " ", "original", " ", "and", " ", "calculated", " ", "cluster", " ", RowBox[{"centers", "."}]}]}], " ", "\[IndentingNewLine]", "*)"}]], "Input", CellChangeTimes->{{3.8222237346358786`*^9, 3.8222237567147245`*^9}},ExpressionUUID->"569870ef-3cf2-44b0-aa8d-\ 306227911a89"] }, Open ]], Cell[CellGroupData[{ Cell["Modules", "Section", CellChangeTimes->{{3.525616140332487*^9, 3.5256161488501024`*^9}, { 3.81086576896773*^9, 3.8108657692390184`*^9}},ExpressionUUID->"a735caa0-6b8e-4179-920e-\ 7fbeb3505d58"], Cell[BoxData[ RowBox[{"Needs", "[", "\"\\"", "]"}]], "Input", CellLabel->"In[3]:=",ExpressionUUID->"5b699cfc-f0e1-4f1b-94eb-504f500b2c16"], Cell[CellGroupData[{ Cell["Objective functions", "Subsection", CellChangeTimes->{ 3.5854097085807176`*^9, 3.585637218737363*^9, 3.6164718039835706`*^9, { 3.6492099748884993`*^9, 3.6492099781333055`*^9}, 3.810729642865247*^9, { 3.821013195169464*^9, 3.8210132020004506`*^9}},ExpressionUUID->"83ccd5bd-7211-4650-8032-\ a07520b98031"], Cell[BoxData[{ RowBox[{ RowBox[{ RowBox[{ RowBox[{"WF", "[", "PI_", "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"m", "=", RowBox[{"Length", "[", RowBox[{"Flatten", "[", RowBox[{"PI", ",", "1"}], "]"}], "]"}]}], ",", "c"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"c", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"Mean", "[", RowBox[{"WeightedData", "[", RowBox[{ RowBox[{"PI", "[", RowBox[{"[", RowBox[{"j", ",", "All", ",", "2"}], "]"}], "]"}], ",", RowBox[{"PI", "[", RowBox[{"[", RowBox[{"j", ",", "All", ",", "1"}], "]"}], "]"}]}], "]"}], "]"}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", RowBox[{"Sum", "[", RowBox[{ RowBox[{ RowBox[{"PI", "[", RowBox[{"[", RowBox[{"j", ",", "s", ",", "1"}], "]"}], "]"}], "*", RowBox[{ RowBox[{"Norm", "[", RowBox[{ RowBox[{"PI", "[", RowBox[{"[", RowBox[{"j", ",", "s", ",", "2"}], "]"}], "]"}], "-", RowBox[{"c", "[", RowBox[{"[", "j", "]"}], "]"}]}], "]"}], "^", "2"}]}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}], " ", ",", RowBox[{"{", RowBox[{"s", ",", RowBox[{"Length", "[", RowBox[{"PI", "[", RowBox[{"[", "j", "]"}], "]"}], "]"}]}], "}"}]}], "]"}], " ", "]"}]}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{ RowBox[{"WG", "[", "PI_", "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"m", "=", RowBox[{"Length", "[", RowBox[{"Flatten", "[", RowBox[{"PI", ",", "1"}], "]"}], "]"}]}], ",", "c", ",", "set", ",", "cc"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"c", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"Mean", "[", RowBox[{"WeightedData", "[", RowBox[{ RowBox[{"PI", "[", RowBox[{"[", RowBox[{"j", ",", "All", ",", "2"}], "]"}], "]"}], ",", RowBox[{"PI", "[", RowBox[{"[", RowBox[{"j", ",", "All", ",", "1"}], "]"}], "]"}]}], "]"}], "]"}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"set", "=", RowBox[{"Flatten", "[", RowBox[{"PI", ",", "1"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"cc", "=", RowBox[{"Mean", "[", RowBox[{"WeightedData", "[", RowBox[{ RowBox[{"set", "[", RowBox[{"[", RowBox[{"All", ",", "2"}], "]"}], "]"}], ",", RowBox[{"set", "[", RowBox[{"[", RowBox[{"All", ",", "1"}], "]"}], "]"}]}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", RowBox[{"Sum", "[", RowBox[{ RowBox[{ RowBox[{"Length", "[", RowBox[{"PI", "[", RowBox[{"[", "j", "]"}], "]"}], "]"}], "*", RowBox[{ RowBox[{"Norm", "[", RowBox[{"cc", "-", RowBox[{"c", "[", RowBox[{"[", "j", "]"}], "]"}]}], "]"}], "^", "2"}]}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}]}], " ", "]"}], "]"}]}]}], "\[IndentingNewLine]", "]"}]}], ";"}], "\[IndentingNewLine]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"WFmin", "[", "PI_", "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"A", "=", RowBox[{ RowBox[{"Flatten", "[", RowBox[{"PI", ",", "1"}], "]"}], "[", RowBox[{"[", RowBox[{"All", ",", "2"}], "]"}], "]"}]}], ",", RowBox[{"k", "=", RowBox[{"Length", "[", "PI", "]"}]}]}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"c", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"Mean", "[", RowBox[{"WeightedData", "[", RowBox[{ RowBox[{"PI", "[", RowBox[{"[", RowBox[{"j", ",", "All", ",", "2"}], "]"}], "]"}], ",", RowBox[{"PI", "[", RowBox[{"[", RowBox[{"j", ",", "All", ",", "1"}], "]"}], "]"}]}], "]"}], "]"}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"Sum", "[", RowBox[{ RowBox[{ RowBox[{ RowBox[{ RowBox[{"Flatten", "[", RowBox[{"PI", ",", "1"}], "]"}], "[", RowBox[{"[", RowBox[{"All", ",", "1"}], "]"}], "]"}], "[", RowBox[{"[", "i", "]"}], "]"}], RowBox[{"Min", "[", RowBox[{"Table", "[", RowBox[{ RowBox[{ RowBox[{"Norm", "[", RowBox[{ RowBox[{"c", "[", RowBox[{"[", "j", "]"}], "]"}], "-", RowBox[{"A", "[", RowBox[{"[", "i", "]"}], "]"}]}], "]"}], "^", "2"}], ",", RowBox[{"{", RowBox[{"j", ",", "k"}], "}"}]}], "]"}], "]"}]}], ",", RowBox[{"{", RowBox[{"i", ",", RowBox[{"Length", "[", "A", "]"}]}], "}"}]}], "]"}]}]}], "\[IndentingNewLine]", "]"}]}]}], "Input", CellChangeTimes->{{3.5886541897699375`*^9, 3.5886541909877396`*^9}, { 3.588654263023868*^9, 3.588654339794004*^9}, {3.5917588683674088`*^9, 3.591758869047409*^9}, {3.5917590328866577`*^9, 3.591759044816675*^9}, { 3.5937444692479725`*^9, 3.5937444771129847`*^9}, {3.6021702358887796`*^9, 3.6021702401943874`*^9}, {3.616471812844386*^9, 3.616471853092457*^9}, { 3.649217497911272*^9, 3.649217504806484*^9}, {3.6492175571133766`*^9, 3.649217557362977*^9}, {3.6492176239438934`*^9, 3.649217665358367*^9}, 3.6492181013323326`*^9, 3.8073355190202684`*^9, 3.8073355895057535`*^9, { 3.810729489618216*^9, 3.8107295166252146`*^9}, {3.810729651271786*^9, 3.810729656081521*^9}, {3.8107298258666334`*^9, 3.810729921897159*^9}, { 3.8107299645876637`*^9, 3.810729973800375*^9}, 3.8131291981921396`*^9}, CellLabel->"In[4]:=",ExpressionUUID->"8b9e9b39-315f-44d0-a90b-7f3df8cbcfd6"] }, Closed]], Cell[CellGroupData[{ Cell["Weighted k - means algorithm\t\t", "Subsection", CellChangeTimes->{{3.525616360604874*^9, 3.5256163644892807`*^9}, 3.5256170782217345`*^9, 3.5262058902050314`*^9, 3.5262060212488317`*^9, 3.526206146804221*^9, {3.5262146495319996`*^9, 3.526214660301216*^9}, { 3.526268235603675*^9, 3.5262682603453183`*^9}, {3.526271497881405*^9, 3.526271504183816*^9}, {3.526272345602494*^9, 3.526272371124139*^9}, { 3.526272761748825*^9, 3.5262727798448563`*^9}, {3.5262733652982845`*^9, 3.5262734414576187`*^9}, {3.526273604665105*^9, 3.5262736247735405`*^9}, 3.526289526950795*^9, {3.5712988897963448`*^9, 3.5712989095705805`*^9}, { 3.5713006259192133`*^9, 3.5713007138277683`*^9}, {3.5713007531946383`*^9, 3.571300771948871*^9}, {3.5713008158719487`*^9, 3.5713008213017583`*^9}, { 3.5713010538319693`*^9, 3.571301059058978*^9}, {3.5713026879974575`*^9, 3.5713027468205614`*^9}, {3.571302831529111*^9, 3.571302841748129*^9}, { 3.5713028969822264`*^9, 3.571302908216246*^9}, {3.571303208091176*^9, 3.571303209541979*^9}, {3.5713032974143333`*^9, 3.5713033007069397`*^9}, { 3.5713034979916883`*^9, 3.5713034993176904`*^9}, {3.571303581655036*^9, 3.5713035867260447`*^9}, {3.571304578139395*^9, 3.5713046065042453`*^9}, { 3.571304657245535*^9, 3.571304680884577*^9}, {3.571310017656273*^9, 3.571310032448099*^9}, {3.571310291879758*^9, 3.571310305546382*^9}, { 3.5713118990221987`*^9, 3.5713118994433994`*^9}, {3.5713119570019007`*^9, 3.5713119573451014`*^9}, 3.5713122470594134`*^9, 3.571365674705989*^9, { 3.57136576008634*^9, 3.57136582210345*^9}, 3.5713658564929104`*^9, { 3.571365903925994*^9, 3.5713659117592077`*^9}, {3.571365953060481*^9, 3.571365988119743*^9}, 3.571366114458166*^9, {3.5713663292159452`*^9, 3.5713664037264786`*^9}, {3.5713664475247555`*^9, 3.5713664652181873`*^9}, {3.583463667516041*^9, 3.5834636763476562`*^9}, { 3.5834637448145776`*^9, 3.5834638126820984`*^9}, {3.583463928036507*^9, 3.583463936587322*^9}, {3.5834640002474346`*^9, 3.5834640785287733`*^9}, { 3.583464283239937*^9, 3.583464301667569*^9}, {3.583464610199915*^9, 3.5834646193279314`*^9}, {3.58346479905805*^9, 3.58346481041787*^9}, { 3.5845924958427024`*^9, 3.584592509620527*^9}, {3.585384188395161*^9, 3.585384194558172*^9}, {3.5853842286668324`*^9, 3.5853842303828354`*^9}, { 3.5853904610529246`*^9, 3.5853905905625544`*^9}, {3.585390754049445*^9, 3.585390759806855*^9}, {3.585390809549743*^9, 3.5853908141215515`*^9}, { 3.5853908518494177`*^9, 3.585390872960255*^9}, {3.585390921657941*^9, 3.5853909252001476`*^9}, {3.585390970713428*^9, 3.585390982041048*^9}, { 3.5853911246397*^9, 3.5853911371851225`*^9}, {3.5853912967752047`*^9, 3.5853913671453285`*^9}, {3.585391510369982*^9, 3.5853915141461887`*^9}, { 3.5853916673824606`*^9, 3.5853916997117176`*^9}, {3.5853917298881707`*^9, 3.585391759222223*^9}, 3.5853919038976784`*^9, {3.585392081138392*^9, 3.585392094275615*^9}, {3.5853921646925397`*^9, 3.5853921652385406`*^9}, { 3.5853922544282985`*^9, 3.5853922707645273`*^9}, 3.5853926411463823`*^9, { 3.585392743689764*^9, 3.5853927724466143`*^9}, {3.585392821861902*^9, 3.585392892625027*^9}, {3.585392980566783*^9, 3.5853929924560037`*^9}, { 3.585393022648057*^9, 3.5853930568971176`*^9}, {3.585393089367775*^9, 3.585393089882576*^9}, {3.585393512453127*^9, 3.5853935139839296`*^9}, { 3.5853940508786793`*^9, 3.585394204396752*^9}, {3.5853942383172116`*^9, 3.5853942783236823`*^9}, {3.585394313274744*^9, 3.5853943259761667`*^9}, { 3.5853974538852997`*^9, 3.5853975400466523`*^9}, {3.585397570571306*^9, 3.585397570884307*^9}, {3.5853976141687837`*^9, 3.58539765707786*^9}, { 3.585398300135197*^9, 3.5853983391120663`*^9}, {3.585398538675419*^9, 3.585398561316059*^9}, {3.5853988501949697`*^9, 3.5853989417095327`*^9}, { 3.5853990606827426`*^9, 3.585399182486558*^9}, {3.5853994070305552`*^9, 3.585399461906852*^9}, {3.585399572267847*^9, 3.5853995737810497`*^9}, { 3.5853999723233557`*^9, 3.585400002031808*^9}, {3.585400050261493*^9, 3.5854000549113016`*^9}, {3.5854000992373805`*^9, 3.5854001568446827`*^9}, {3.5854012476180143`*^9, 3.5854012947234974`*^9}, {3.5854014618211927`*^9, 3.585401501578063*^9}, { 3.5854022421005745`*^9, 3.5854022449407797`*^9}, {3.5854023168251066`*^9, 3.585402336141941*^9}, {3.5854024165714836`*^9, 3.585402484833604*^9}, { 3.585408314941718*^9, 3.5854083322551494`*^9}, {3.5854083697334156`*^9, 3.585408392125257*^9}, {3.5854084715109262`*^9, 3.5854084897045584`*^9}, { 3.585408900137684*^9, 3.5854089006368847`*^9}, {3.5854090232785025`*^9, 3.5854090238401036`*^9}, {3.5854091870407925`*^9, 3.585409229653468*^9}, { 3.5854097244021454`*^9, 3.585409746340784*^9}, {3.5856371040607595`*^9, 3.5856371057309628`*^9}, 3.588251133089259*^9, 3.588654178816718*^9, { 3.649209957073268*^9, 3.6492099581496696`*^9}, {3.821013213528505*^9, 3.821013216871851*^9}},ExpressionUUID->"028b9148-f5bb-4e61-8442-\ e1c7a2fad965"], Cell[BoxData[ RowBox[{ RowBox[{"WKMeans", "[", RowBox[{"w_", ",", "A_", ",", "z_", ",", RowBox[{"Ind_:", "0"}]}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"m", "=", RowBox[{"Length", "[", "A", "]"}]}], ",", RowBox[{"k", "=", RowBox[{"Length", "[", "z", "]"}]}], ",", "c0", ",", "Pod", ",", "imin", ",", "PI", ",", RowBox[{"It", "=", "0"}]}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"Pod", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"w", "[", RowBox[{"[", "i", "]"}], "]"}], ",", RowBox[{"A", "[", RowBox[{"[", "i", "]"}], "]"}]}], "}"}], ",", RowBox[{"{", RowBox[{"i", ",", "m"}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"c0", "=", "z"}], ";", " ", "\[IndentingNewLine]", RowBox[{"PI", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"{", "}"}], ",", RowBox[{"{", RowBox[{"i", ",", "k"}], "}"}]}], "]"}]}], ";", " ", "\[IndentingNewLine]", "\t\t\t", RowBox[{"(*", " ", RowBox[{"Minimal", " ", "distance", " ", "principle"}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"tab", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"Norm", "[", RowBox[{ RowBox[{"Pod", "[", RowBox[{"[", RowBox[{"i", ",", "2"}], "]"}], "]"}], "-", RowBox[{"c0", "[", RowBox[{"[", "j", "]"}], "]"}]}], "]"}], ",", RowBox[{"{", RowBox[{"j", ",", "k"}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"imin", "=", RowBox[{ RowBox[{"Ordering", "[", RowBox[{"tab", ",", "1"}], "]"}], "[", RowBox[{"[", "1", "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"PI", "[", RowBox[{"[", "imin", "]"}], "]"}], "=", RowBox[{"Append", "[", RowBox[{ RowBox[{"PI", "[", RowBox[{"[", "imin", "]"}], "]"}], ",", RowBox[{"Pod", "[", RowBox[{"[", "i", "]"}], "]"}]}], "]"}]}]}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"i", ",", "m"}], "}"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"PI", "=", RowBox[{"DeleteCases", "[", RowBox[{"PI", ",", RowBox[{"{", "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"k", "=", RowBox[{"Length", "[", "PI", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"Ind", "\[NotEqual]", " ", "0"}], ",", RowBox[{"Print", "[", RowBox[{"It", ",", "\"\<: F = \>\"", ",", RowBox[{"WF", "[", "PI", "]"}]}], "]"}]}], "]"}], ";", "\[IndentingNewLine]", "\t\t\t\t\t\t", RowBox[{"(*", " ", RowBox[{"New", " ", "centers"}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"While", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"c", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"Mean", "[", RowBox[{"WeightedData", "[", RowBox[{ RowBox[{"PI", "[", RowBox[{"[", RowBox[{"j", ",", "All", ",", "2"}], "]"}], "]"}], ",", RowBox[{"PI", "[", RowBox[{"[", RowBox[{"j", ",", "All", ",", "1"}], "]"}], "]"}]}], "]"}], "]"}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}]}], "]"}]}], ";", " ", "\[IndentingNewLine]", "\t\t", RowBox[{"(*", " ", RowBox[{ RowBox[{ "The", " ", "case", " ", "of", " ", "reducing", " ", "the", " ", "number", " ", "of", " ", "clusters", " ", "in", " ", "a", " ", "partition", "\[IndentingNewLine]", "\t\t\t\t", "in", " ", "the", " ", "case", " ", "n"}], "=", RowBox[{"2", " ", "-", " ", RowBox[{ "can", " ", "be", " ", "extended", " ", "to", " ", "an", " ", "arbitrary", " ", "n"}]}]}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"Length", "[", "c", "]"}], "<", RowBox[{"Length", "[", "c0", "]"}]}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", RowBox[{"Length", "[", "c0", "]"}], ",", "\"\< -> \>\"", ",", RowBox[{"Length", "[", "c", "]"}], ",", "\"\<}\\nTry with a new initial centers\>\""}], "]"}], ";"}]}], "\[IndentingNewLine]", "\t", "]"}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"Chop", "[", RowBox[{"Norm", "[", RowBox[{"c", "-", "c0"}], "]"}], "]"}], "\[NotEqual]", "0"}]}], ",", RowBox[{ RowBox[{"c0", "=", "c"}], ";", RowBox[{"It", "=", RowBox[{"It", "+", "1"}]}], ";", "\[IndentingNewLine]", "\t\t\t\t", RowBox[{"(*", " ", RowBox[{"New", " ", "partition"}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"PI", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"{", "}"}], ",", RowBox[{"{", RowBox[{"i", ",", "k"}], "}"}]}], "]"}]}], ";", " ", "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"tab", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"Norm", "[", RowBox[{ RowBox[{"Pod", "[", RowBox[{"[", RowBox[{"i", ",", "2"}], "]"}], "]"}], "-", RowBox[{"c0", "[", RowBox[{"[", "j", "]"}], "]"}]}], "]"}], ",", RowBox[{"{", RowBox[{"j", ",", "k"}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"imin", "=", RowBox[{ RowBox[{"Ordering", "[", RowBox[{"tab", ",", "1"}], "]"}], "[", RowBox[{"[", "1", "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"PI", "[", RowBox[{"[", "imin", "]"}], "]"}], "=", RowBox[{"Append", "[", RowBox[{ RowBox[{"PI", "[", RowBox[{"[", "imin", "]"}], "]"}], ",", RowBox[{"Pod", "[", RowBox[{"[", "i", "]"}], "]"}]}], "]"}]}]}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"i", ",", "m"}], "}"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"PI", "=", RowBox[{"DeleteCases", "[", RowBox[{"PI", ",", RowBox[{"{", "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"k", "=", RowBox[{"Length", "[", "PI", "]"}]}], ";", " ", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"Ind", "\[NotEqual]", " ", "0"}], ",", RowBox[{"Print", "[", RowBox[{"It", ",", "\"\<: F = \>\"", ",", RowBox[{"WF", "[", "PI", "]"}]}], "]"}]}], "]"}], ";"}]}], "\[IndentingNewLine]", " ", "]"}], ";", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"PI", ",", "c0", ",", RowBox[{"WF", "[", "PI", "]"}], ",", "It"}], "}"}]}]}], "]"}]}]], "Input", CellChangeTimes->{{3.827114567512955*^9, 3.8271146294897485`*^9}, { 3.8271147440375824`*^9, 3.8271147470680733`*^9}, 3.8271157617443647`*^9, { 3.8271158884091043`*^9, 3.827115931076314*^9}, {3.827117308614561*^9, 3.827117310329851*^9}, {3.827118673169304*^9, 3.827118678510786*^9}}, CellLabel->"In[7]:=",ExpressionUUID->"88b75466-5866-4b8b-b3ba-b034636edd6b"] }, Open ]], Cell[CellGroupData[{ Cell["Incremental algorithm", "Subsection", CellChangeTimes->{{3.807410659943698*^9, 3.807410671560322*^9}},ExpressionUUID->"9492ea96-34ed-4341-bfeb-\ f3f8409acd87"], Cell[BoxData[ RowBox[{ RowBox[{"Inc", "[", RowBox[{"w_", ",", "A_", ",", "cen_", ",", "K_", ",", RowBox[{"Ind_:", "0"}]}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"k", ",", "centri", ",", "centriN", ",", RowBox[{"m", "=", RowBox[{"Length", "[", "A", "]"}]}], ",", RowBox[{"n", "=", RowBox[{"Length", "[", RowBox[{"A", "[", RowBox[{"[", "1", "]"}], "]"}], "]"}]}], ",", "dom"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"AG", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{ RowBox[{"Min", "[", RowBox[{"A", "[", RowBox[{"[", RowBox[{"All", ",", "i"}], "]"}], "]"}], "]"}], "-", ".5"}], ",", RowBox[{ RowBox[{"Max", "[", RowBox[{"A", "[", RowBox[{"[", RowBox[{"All", ",", "i"}], "]"}], "]"}], "]"}], "+", ".5"}]}], "}"}], ",", RowBox[{"{", RowBox[{"i", ",", "n"}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", "\t\t\t\t\t", RowBox[{"(*", " ", "Initialization", " ", "*)"}], "\[IndentingNewLine]", RowBox[{"centri", "=", "cen"}], ";", "\[IndentingNewLine]", RowBox[{"PART", "=", RowBox[{"{", "}"}]}], ";", RowBox[{"IndCH", "=", RowBox[{"IndDB", "=", RowBox[{"IndSSWC", "=", RowBox[{"IndDunn", "=", RowBox[{"IndH", "=", RowBox[{"{", "}"}]}]}]}]}]}], ";", "\[IndentingNewLine]", "\t\t\t\t", RowBox[{"(*", " ", RowBox[{"Iterative", " ", "procedure"}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\<\\nTo existing \>\"", ",", RowBox[{"Length", "[", "centri", "]"}], ",", "\"\< centers one new is added\>\""}], "]"}], ";", "\[IndentingNewLine]", "\t", RowBox[{"(*", " ", RowBox[{ RowBox[{"Compile", " ", "in", " ", "the", " ", "case", " ", "n"}], "=", RowBox[{"2", " ", "-", " ", RowBox[{ "can", " ", "be", " ", "extended", " ", "to", " ", "an", " ", "arbitrary", " ", "n"}]}]}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"Clear", "[", RowBox[{"u", ",", "v"}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"FCompile", "=", RowBox[{"Compile", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"u", ",", "_Real"}], "}"}], ",", RowBox[{"{", RowBox[{"v", ",", "_Real"}], "}"}]}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"Evaluate", "[", RowBox[{"Sum", "[", RowBox[{ RowBox[{ RowBox[{"w", "[", RowBox[{"[", "i", "]"}], "]"}], RowBox[{"Min", "[", RowBox[{"Append", "[", RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{ RowBox[{"Norm", "[", RowBox[{ RowBox[{"A", "[", RowBox[{"[", "i", "]"}], "]"}], "-", RowBox[{"centri", "[", RowBox[{"[", "s", "]"}], "]"}]}], "]"}], "^", "2"}], ",", RowBox[{"{", RowBox[{"s", ",", RowBox[{"Length", "[", "centri", "]"}]}], "}"}]}], "]"}], ",", RowBox[{ RowBox[{"Norm", "[", RowBox[{ RowBox[{"A", "[", RowBox[{"[", "i", "]"}], "]"}], "-", RowBox[{"{", RowBox[{"u", ",", "v"}], "}"}]}], "]"}], "^", "2"}]}], "]"}], "]"}]}], ",", RowBox[{"{", RowBox[{"i", ",", "m"}], "}"}]}], "]"}], " ", "]"}]}], "]"}]}], ";", "\t", "\[IndentingNewLine]", RowBox[{ RowBox[{"F2", "[", RowBox[{"u_", ",", "v_"}], "]"}], ":=", RowBox[{"Apply", "[", RowBox[{"FCompile", ",", RowBox[{"{", RowBox[{"u", ",", "v"}], "}"}]}], "]"}]}], ";", "\t", "\[IndentingNewLine]", "\t\t\t\t\t\t", RowBox[{"(*", " ", RowBox[{ RowBox[{ RowBox[{"Non", " ", "Compile", " ", "\[IndentingNewLine]", RowBox[{"F2", "[", RowBox[{"u_", ",", "v_"}], "]"}]}], ":=", RowBox[{"Sum", "[", RowBox[{ RowBox[{ RowBox[{"w", "[", RowBox[{"[", "i", "]"}], "]"}], RowBox[{"Min", "[", RowBox[{"Append", "[", RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{ RowBox[{"Norm", "[", RowBox[{ RowBox[{"A", "[", RowBox[{"[", "i", "]"}], "]"}], "-", RowBox[{"centri", "[", RowBox[{"[", "s", "]"}], "]"}]}], "]"}], "^", "2"}], ",", RowBox[{"{", RowBox[{"s", ",", RowBox[{"Length", "[", "centri", "]"}]}], "}"}]}], "]"}], ",", RowBox[{ RowBox[{"Norm", "[", RowBox[{ RowBox[{"A", "[", RowBox[{"[", "i", "]"}], "]"}], "-", RowBox[{"{", RowBox[{"u", ",", "v"}], "}"}]}], "]"}], "^", "2"}]}], "]"}], "]"}]}], ",", RowBox[{"{", RowBox[{"i", ",", "m"}], "}"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", "*)"}], "\[IndentingNewLine]", "\t\t\t\t\t", RowBox[{"(*", " ", RowBox[{ RowBox[{"Applying", " ", "of", " ", "the", " ", "DIRECT"}], "-", "algorithm"}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"dom", "=", "AG"}], ";", "\[IndentingNewLine]", RowBox[{"minp", "=", RowBox[{ RowBox[{"DIRECT", "[", RowBox[{"F2", ",", "dom", ",", RowBox[{"DMin", "\[Rule]", RowBox[{"10.", "^", RowBox[{"(", RowBox[{"-", "3"}], ")"}]}]}], ",", RowBox[{"MaxIter", "\[Rule]", "50"}], ",", " ", RowBox[{"Eps", "\[Rule]", RowBox[{"10", "^", RowBox[{"(", RowBox[{"-", "4"}], ")"}]}]}]}], "]"}], "//", "Timing"}]}], ";", " ", RowBox[{"min", "=", RowBox[{"minp", "[", RowBox[{"[", "2", "]"}], "]"}]}], ";", RowBox[{"(*", " ", RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\\"", ",", "min"}], "]"}], ";"}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"centriN", "=", RowBox[{"Append", "[", RowBox[{"centri", ",", RowBox[{"min", "[", RowBox[{"[", "1", "]"}], "]"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"Ind", "\[NotEqual]", "0"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", "centriN"}], "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"Ind", "\[NotEqual]", "0"}], ",", RowBox[{"slcen", "=", RowBox[{"ListPlot", "[", RowBox[{"centriN", ",", RowBox[{"PlotMarkers", "\[Rule]", RowBox[{"{", RowBox[{"\"\<\[SixPointedStar]\>\"", ",", "20"}], "}"}]}], ",", RowBox[{"PlotStyle", "\[Rule]", RowBox[{"{", RowBox[{"Gray", ",", RowBox[{"PointSize", "[", ".025", "]"}], ",", RowBox[{"Opacity", "[", ".7", "]"}]}], "}"}]}]}], "]"}]}]}], "]"}], ";", "\[IndentingNewLine]", "\t\t\t\t\t", RowBox[{"(*", " ", RowBox[{"Call", " ", "WKMeans"}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"t1", "=", RowBox[{"Timing", "[", RowBox[{"sol", "=", RowBox[{"WKMeans", "[", RowBox[{"w", ",", "A", ",", "centriN"}], "]"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"k", "=", RowBox[{"Length", "[", RowBox[{"sol", "[", RowBox[{"[", "1", "]"}], "]"}], "]"}]}], ";", " ", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"Ind", "\[NotEqual]", "0"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"sol", "[", RowBox[{"[", "2", "]"}], "]"}]}], "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"Ind", "\[NotEqual]", "0"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"{", RowBox[{ RowBox[{"WF", "[", RowBox[{"sol", "[", RowBox[{"[", "1", "]"}], "]"}], "]"}], ",", RowBox[{"WG", "[", RowBox[{"sol", "[", RowBox[{"[", "1", "]"}], "]"}], "]"}]}], "}"}], ",", "\"\<; It=\>\"", ",", RowBox[{"sol", "[", RowBox[{"[", "4", "]"}], "]"}], ",", "\"\<; mj: \>\"", ",", RowBox[{"Table", "[", RowBox[{ RowBox[{"Length", "[", RowBox[{"sol", "[", RowBox[{"[", RowBox[{"1", ",", "j"}], "]"}], "]"}], "]"}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", RowBox[{"sol", "[", RowBox[{"[", "1", "]"}], "]"}], "]"}]}], "}"}]}], "]"}]}], "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"Ind", "\[NotEqual]", "0"}], ",", RowBox[{"Print", "[", RowBox[{"\"\