text:
export Block

Block(id::Int) = Block(id, 0)

const AIR                 = Block(0)
const STONE               = Block(1)
const GRASS               = Block(2)
const DIRT                = Block(3)
const COBBLESTONE         = Block(4)
const WOOD_PLANKS         = Block(5)
const SAPLING             = Block(6)
const BEDROCK             = Block(7)
const WATER_FLOWING       = Block(8)
const WATER               = WATER_FLOWING
const WATER_STATIONARY    = Block(9)
const LAVA_FLOWING        = Block(10)
const LAVA                = LAVA_FLOWING
const LAVA_STATIONARY     = Block(11)
const SAND                = Block(12)
const GRAVEL              = Block(13)
const GOLD_ORE            = Block(14)
const IRON_ORE            = Block(15)
const COAL_ORE            = Block(16)
const WOOD                = Block(17)
const LEAVES              = Block(18)
const GLASS               = Block(20)
const LAPIS_LAZULI_ORE    = Block(21)
const LAPIS_LAZULI_BLOCK  = Block(22)
const SANDSTONE           = Block(24)
const BED                 = Block(26)
const COBWEB              = Block(30)
const GRASS_TALL          = Block(31)
const WOOL                = Block(35)
const FLOWER_YELLOW       = Block(37)
const FLOWER_CYAN         = Block(38)
const MUSHROOM_BROWN      = Block(39)
const MUSHROOM_RED        = Block(40)
const GOLD_BLOCK          = Block(41)
const IRON_BLOCK          = Block(42)
const STONE_SLAB_DOUBLE   = Block(43)
const STONE_SLAB          = Block(44)
const BRICK_BLOCK         = Block(45)
const TNT                 = Block(46)
const BOOKSHELF           = Block(47)
const MOSS_STONE          = Block(48)
const OBSIDIAN            = Block(49)
const TORCH               = Block(50)
const FIRE                = Block(51)
const STAIRS_WOOD         = Block(53)
const CHEST               = Block(54)
const DIAMOND_ORE         = Block(56)
const DIAMOND_BLOCK       = Block(57)
const CRAFTING_TABLE      = Block(58)
const FARMLAND            = Block(60)
const FURNACE_INACTIVE    = Block(61)
const FURNACE_ACTIVE      = Block(62)
const DOOR_WOOD           = Block(64)
const LADDER              = Block(65)
const STAIRS_COBBLESTONE  = Block(67)
const DOOR_IRON           = Block(71)
const REDSTONE_ORE        = Block(73)
const SNOW                = Block(78)
const ICE                 = Block(79)
const SNOW_BLOCK          = Block(80)
const CACTUS              = Block(81)
const CLAY                = Block(82)
const SUGAR_CANE          = Block(83)
const FENCE               = Block(85)
const GLOWSTONE_BLOCK     = Block(89)
const BEDROCK_INVISIBLE   = Block(95)
const STONE_BRICK         = Block(98)
const GLASS_PANE          = Block(102)
const MELON               = Block(103)
const FENCE_GATE          = Block(107)
const GLOWING_OBSIDIAN    = Block(246)
const NETHER_REACTOR_CORE = Block(247)

const mc_items = Dict{String, Block}()
mc_items["Air"]=Block(0,0)
mc_items["Stone"]=Block(1,0)
mc_items["Granite"]=Block(1,1)
mc_items["Polished Granite"]=Block(1,2)
mc_items["Diorite"]=Block(1,3)
mc_items["Polished Diorite"]=Block(1,4)
mc_items["Andesite"]=Block(1,5)
mc_items["Polished Andesite"]=Block(1,6)
mc_items["Grass"]=Block(2,0)
mc_items["Dirt"]=Block(3,0)
mc_items["Coarse Dirt"]=Block(3,1)
mc_items["Podzol"]=Block(3,2)
mc_items["Cobblestone"]=Block(4,0)
mc_items["Oak Wood Plank"]=Block(5,0)
mc_items["Spruce Wood Plank"]=Block(5,1)
mc_items["Birch Wood Plank"]=Block(5,2)
mc_items["Jungle Wood Plank"]=Block(5,3)
mc_items["Acacia Wood Plank"]=Block(5,4)
mc_items["Dark Oak Wood Plank"]=Block(5,5)
mc_items["Oak Sapling"]=Block(6,0)
mc_items["Spruce Sapling"]=Block(6,1)
mc_items["Birch Sapling"]=Block(6,2)
mc_items["Jungle Sapling"]=Block(6,3)
mc_items["Acacia Sapling"]=Block(6,4)
mc_items["Dark Oak Sapling"]=Block(6,5)
mc_items["Bedrock"]=Block(7,0)
mc_items["Flowing Water"]=Block(8,0)
mc_items["Still Water"]=Block(9,0)
mc_items["Flowing Lava"]=Block(10,0)
mc_items["Still Lava"]=Block(11,0)
mc_items["Sand"]=Block(12,0)
mc_items["Red Sand"]=Block(12,1)
mc_items["Gravel"]=Block(13,0)
mc_items["Gold Ore"]=Block(14,0)
mc_items["Iron Ore"]=Block(15,0)
mc_items["Coal Ore"]=Block(16,0)
mc_items["Oak Wood"]=Block(17,0)
mc_items["Spruce Wood"]=Block(17,1)
mc_items["Birch Wood"]=Block(17,2)
mc_items["Jungle Wood"]=Block(17,3)
mc_items["Oak Leaves"]=Block(18,0)
mc_items["Spruce Leaves"]=Block(18,1)
mc_items["Birch Leaves"]=Block(18,2)
mc_items["Jungle Leaves"]=Block(18,3)
mc_items["Sponge"]=Block(19,0)
mc_items["Wet Sponge"]=Block(19,1)
mc_items["Glass"]=Block(20,0)
mc_items["Lapis Lazuli Ore"]=Block(21,0)
mc_items["Lapis Lazuli Block"]=Block(22,0)
mc_items["Dispenser"]=Block(23,0)
mc_items["Sandstone"]=Block(24,0)
mc_items["Chiseled Sandstone"]=Block(24,1)
mc_items["Smooth Sandstone"]=Block(24,2)
mc_items["Note Block"]=Block(25,0)
mc_items["Bed"]=Block(26,0)
mc_items["Powered Rail"]=Block(27,0)
mc_items["Detector Rail"]=Block(28,0)
mc_items["Sticky Piston"]=Block(29,0)
mc_items["Cobweb"]=Block(30,0)
mc_items["Dead Shrub"]=Block(31,0)
mc_items["Grass"]=Block(31,1)
mc_items["Fern"]=Block(31,2)
mc_items["Dead Bush"]=Block(32,0)
mc_items["Piston"]=Block(33,0)
mc_items["Piston Head"]=Block(34,0)
mc_items["White Wool"]=Block(35,0)
mc_items["Orange Wool"]=Block(35,1)
mc_items["Magenta Wool"]=Block(35,2)
mc_items["Light Blue Wool"]=Block(35,3)
mc_items["Yellow Wool"]=Block(35,4)
mc_items["Lime Wool"]=Block(35,5)
mc_items["Pink Wool"]=Block(35,6)
mc_items["Gray Wool"]=Block(35,7)
mc_items["Light Gray Wool"]=Block(35,8)
mc_items["Cyan Wool"]=Block(35,9)
mc_items["Purple Wool"]=Block(35,10)
mc_items["Blue Wool"]=Block(35,11)
mc_items["Brown Wool"]=Block(35,12)
mc_items["Green Wool"]=Block(35,13)
mc_items["Red Wool"]=Block(35,14)
mc_items["Black Wool"]=Block(35,15)
mc_items["Dandelion"]=Block(37,0)
mc_items["Poppy"]=Block(38,0)
mc_items["Blue Orchid"]=Block(38,1)
mc_items["Allium"]=Block(38,2)
mc_items["Azure Bluet"]=Block(38,3)
mc_items["Red Tulip"]=Block(38,4)
mc_items["Orange Tulip"]=Block(38,5)
mc_items["White Tulip"]=Block(38,6)
mc_items["Pink Tulip"]=Block(38,7)
mc_items["Oxeye Daisy"]=Block(38,8)
mc_items["Brown Mushroom"]=Block(39,0)
mc_items["Red Mushroom"]=Block(40,0)
mc_items["Gold Block"]=Block(41,0)
mc_items["Iron Block"]=Block(42,0)
mc_items["Double Stone Slab"]=Block(43,0)
mc_items["Double Sandstone Slab"]=Block(43,1)
mc_items["Double Wooden Slab"]=Block(43,2)
mc_items["Double Cobblestone Slab"]=Block(43,3)
mc_items["Double Brick Slab"]=Block(43,4)
mc_items["Double Stone Brick Slab"]=Block(43,5)
mc_items["Double Nether Brick Slab"]=Block(43,6)
mc_items["Double Quartz Slab"]=Block(43,7)
mc_items["Stone Slab"]=Block(44,0)
mc_items["Sandstone Slab"]=Block(44,1)
mc_items["Wooden Slab"]=Block(44,2)
mc_items["Cobblestone Slab"]=Block(44,3)
mc_items["Brick Slab"]=Block(44,4)
mc_items["Stone Brick Slab"]=Block(44,5)
mc_items["Nether Brick Slab"]=Block(44,6)
mc_items["Quartz Slab"]=Block(44,7)
mc_items["Bricks"]=Block(45,0)
mc_items["TNT"]=Block(46,0)
mc_items["Bookshelf"]=Block(47,0)
mc_items["Moss Stone"]=Block(48,0)
mc_items["Obsidian"]=Block(49,0)
mc_items["Torch"]=Block(50,0)
mc_items["Fire"]=Block(51,0)
mc_items["Monster Spawner"]=Block(52,0)
mc_items["Oak Wood Stairs"]=Block(53,0)
mc_items["Chest"]=Block(54,0)
mc_items["Redstone Wire"]=Block(55,0)
mc_items["Diamond Ore"]=Block(56,0)
mc_items["Diamond Block"]=Block(57,0)
mc_items["Crafting Table"]=Block(58,0)
mc_items["Wheat Crops"]=Block(59,0)
mc_items["Farmland"]=Block(60,0)
mc_items["Furnace"]=Block(61,0)
mc_items["Burning Furnace"]=Block(62,0)
mc_items["Standing Sign Block"]=Block(63,0)
mc_items["Oak Door Block"]=Block(64,0)
mc_items["Ladder"]=Block(65,0)
mc_items["Rail"]=Block(66,0)
mc_items["Cobblestone Stairs"]=Block(67,0)
mc_items["Wall-mounted Sign Block"]=Block(68,0)
mc_items["Lever"]=Block(69,0)
mc_items["Stone Pressure Plate"]=Block(70,0)
mc_items["Iron Door Block"]=Block(71,0)
mc_items["Wooden Pressure Plate"]=Block(72,0)
mc_items["Redstone Ore"]=Block(73,0)
mc_items["Glowing Redstone Ore"]=Block(74,0)
mc_items["Redstone Torch (off)"]=Block(75,0)
mc_items["Redstone Torch (on)"]=Block(76,0)
mc_items["Stone Button"]=Block(77,0)
mc_items["Snow"]=Block(78,0)
mc_items["Ice"]=Block(79,0)
mc_items["Snow Block"]=Block(80,0)
mc_items["Cactus"]=Block(81,0)
mc_items["Clay"]=Block(82,0)
mc_items["Sugar Canes"]=Block(83,0)
mc_items["Jukebox"]=Block(84,0)
mc_items["Oak Fence"]=Block(85,0)
mc_items["Pumpkin"]=Block(86,0)
mc_items["Netherrack"]=Block(87,0)
mc_items["Soul Sand"]=Block(88,0)
mc_items["Glowstone"]=Block(89,0)
mc_items["Nether Portal"]=Block(90,0)
mc_items["Jack o'Lantern"]=Block(91,0)
mc_items["Cake Block"]=Block(92,0)
mc_items["Redstone Repeater Block (off)"]=Block(93,0)
mc_items["Redstone Repeater Block (on)"]=Block(94,0)
mc_items["White Stained Glass"]=Block(95,0)
mc_items["Orange Stained Glass"]=Block(95,1)
mc_items["Magenta Stained Glass"]=Block(95,2)
mc_items["Light Blue Stained Glass"]=Block(95,3)
mc_items["Yellow Stained Glass"]=Block(95,4)
mc_items["Lime Stained Glass"]=Block(95,5)
mc_items["Pink Stained Glass"]=Block(95,6)
mc_items["Gray Stained Glass"]=Block(95,7)
mc_items["Light Gray Stained Glass"]=Block(95,8)
mc_items["Cyan Stained Glass"]=Block(95,9)
mc_items["Purple Stained Glass"]=Block(95,10)
mc_items["Blue Stained Glass"]=Block(95,11)
mc_items["Brown Stained Glass"]=Block(95,12)
mc_items["Green Stained Glass"]=Block(95,13)
mc_items["Red Stained Glass"]=Block(95,14)
mc_items["Black Stained Glass"]=Block(95,15)
mc_items["Wooden Trapdoor"]=Block(96,0)
mc_items["Stone Monster Egg"]=Block(97,0)
mc_items["Cobblestone Monster Egg"]=Block(97,1)
mc_items["Stone Brick Monster Egg"]=Block(97,2)
mc_items["Mossy Stone Brick Monster Egg"]=Block(97,3)
mc_items["Cracked Stone Brick Monster Egg"]=Block(97,4)
mc_items["Chiseled Stone Brick Monster Egg"]=Block(97,5)
mc_items["Stone Bricks"]=Block(98,0)
mc_items["Mossy Stone Bricks"]=Block(98,1)
mc_items["Cracked Stone Bricks"]=Block(98,2)
mc_items["Chiseled Stone Bricks"]=Block(98,3)
mc_items["Brown Mushroom Block"]=Block(99,0)
mc_items["Red Mushroom Block"]=Block(100,0)
mc_items["Iron Bars"]=Block(101,0)
mc_items["Glass Pane"]=Block(102,0)
mc_items["Melon Block"]=Block(103,0)
mc_items["Pumpkin Stem"]=Block(104,0)
mc_items["Melon Stem"]=Block(105,0)
mc_items["Vines"]=Block(106,0)
mc_items["Oak Fence Gate"]=Block(107,0)
mc_items["Brick Stairs"]=Block(108,0)
mc_items["Stone Brick Stairs"]=Block(109,0)
mc_items["Mycelium"]=Block(110,0)
mc_items["Lily Pad"]=Block(111,0)
mc_items["Nether Brick"]=Block(112,0)
mc_items["Nether Brick Fence"]=Block(113,0)
mc_items["Nether Brick Stairs"]=Block(114,0)
mc_items["Nether Wart"]=Block(115,0)
mc_items["Enchantment Table"]=Block(116,0)
mc_items["Brewing Stand"]=Block(117,0)
mc_items["Cauldron"]=Block(118,0)
mc_items["End Portal"]=Block(119,0)
mc_items["End Portal Frame"]=Block(120,0)
mc_items["End Stone"]=Block(121,0)
mc_items["Dragon Egg"]=Block(122,0)
mc_items["Redstone Lamp (inactive)"]=Block(123,0)
mc_items["Redstone Lamp (active)"]=Block(124,0)
mc_items["Double Oak Wood Slab"]=Block(125,0)
mc_items["Double Spruce Wood Slab"]=Block(125,1)
mc_items["Double Birch Wood Slab"]=Block(125,2)
mc_items["Double Jungle Wood Slab"]=Block(125,3)
mc_items["Double Acacia Wood Slab"]=Block(125,4)
mc_items["Double Dark Oak Wood Slab"]=Block(125,5)
mc_items["Oak Wood Slab"]=Block(126,0)
mc_items["Spruce Wood Slab"]=Block(126,1)
mc_items["Birch Wood Slab"]=Block(126,2)
mc_items["Jungle Wood Slab"]=Block(126,3)
mc_items["Acacia Wood Slab"]=Block(126,4)
mc_items["Dark Oak Wood Slab"]=Block(126,5)
mc_items["Cocoa"]=Block(127,0)
mc_items["Sandstone Stairs"]=Block(128,0)
mc_items["Emerald Ore"]=Block(129,0)
mc_items["Ender Chest"]=Block(130,0)
mc_items["Tripwire Hook"]=Block(131,0)
mc_items["Tripwire"]=Block(132,0)
mc_items["Emerald Block"]=Block(133,0)
mc_items["Spruce Wood Stairs"]=Block(134,0)
mc_items["Birch Wood Stairs"]=Block(135,0)
mc_items["Jungle Wood Stairs"]=Block(136,0)
mc_items["Command Block"]=Block(137,0)
mc_items["Beacon"]=Block(138,0)
mc_items["Cobblestone Wall"]=Block(139,0)
mc_items["Mossy Cobblestone Wall"]=Block(139,1)
mc_items["Flower Pot"]=Block(140,0)
mc_items["Carrots"]=Block(141,0)
mc_items["Potatoes"]=Block(142,0)
mc_items["Wooden Button"]=Block(143,0)
mc_items["Mob Head"]=Block(144,0)
mc_items["Anvil"]=Block(145,0)
mc_items["Trapped Chest"]=Block(146,0)
mc_items["Weighted Pressure Plate (light)"]=Block(147,0)
mc_items["Weighted Pressure Plate (heavy)"]=Block(148,0)
mc_items["Redstone Comparator (inactive)"]=Block(149,0)
mc_items["Redstone Comparator (active)"]=Block(150,0)
mc_items["Daylight Sensor"]=Block(151,0)
mc_items["Redstone Block"]=Block(152,0)
mc_items["Nether Quartz Ore"]=Block(153,0)
mc_items["Hopper"]=Block(154,0)
mc_items["Quartz Block"]=Block(155,0)
mc_items["Chiseled Quartz Block"]=Block(155,1)
mc_items["Pillar Quartz Block"]=Block(155,2)
mc_items["Quartz Stairs"]=Block(156,0)
mc_items["Activator Rail"]=Block(157,0)
mc_items["Dropper"]=Block(158,0)
mc_items["White Hardened Clay"]=Block(159,0)
mc_items["Orange Hardened Clay"]=Block(159,1)
mc_items["Magenta Hardened Clay"]=Block(159,2)
mc_items["Light Blue Hardened Clay"]=Block(159,3)
mc_items["Yellow Hardened Clay"]=Block(159,4)
mc_items["Lime Hardened Clay"]=Block(159,5)
mc_items["Pink Hardened Clay"]=Block(159,6)
mc_items["Gray Hardened Clay"]=Block(159,7)
mc_items["Light Gray Hardened Clay"]=Block(159,8)
mc_items["Cyan Hardened Clay"]=Block(159,9)
mc_items["Purple Hardened Clay"]=Block(159,10)
mc_items["Blue Hardened Clay"]=Block(159,11)
mc_items["Brown Hardened Clay"]=Block(159,12)
mc_items["Green Hardened Clay"]=Block(159,13)
mc_items["Red Hardened Clay"]=Block(159,14)
mc_items["Black Hardened Clay"]=Block(159,15)
mc_items["White Stained Glass Pane"]=Block(160,0)
mc_items["Orange Stained Glass Pane"]=Block(160,1)
mc_items["Magenta Stained Glass Pane"]=Block(160,2)
mc_items["Light Blue Stained Glass Pane"]=Block(160,3)
mc_items["Yellow Stained Glass Pane"]=Block(160,4)
mc_items["Lime Stained Glass Pane"]=Block(160,5)
mc_items["Pink Stained Glass Pane"]=Block(160,6)
mc_items["Gray Stained Glass Pane"]=Block(160,7)
mc_items["Light Gray Stained Glass Pane"]=Block(160,8)
mc_items["Cyan Stained Glass Pane"]=Block(160,9)
mc_items["Purple Stained Glass Pane"]=Block(160,10)
mc_items["Blue Stained Glass Pane"]=Block(160,11)
mc_items["Brown Stained Glass Pane"]=Block(160,12)
mc_items["Green Stained Glass Pane"]=Block(160,13)
mc_items["Red Stained Glass Pane"]=Block(160,14)
mc_items["Black Stained Glass Pane"]=Block(160,15)
mc_items["Acacia Leaves"]=Block(161,0)
mc_items["Dark Oak Leaves"]=Block(161,1)
mc_items["Acacia Wood"]=Block(162,0)
mc_items["Dark Oak Wood"]=Block(162,1)
mc_items["Acacia Wood Stairs"]=Block(163,0)
mc_items["Dark Oak Wood Stairs"]=Block(164,0)
mc_items["Slime Block"]=Block(165,0)
mc_items["Barrier"]=Block(166,0)
mc_items["Iron Trapdoor"]=Block(167,0)
mc_items["Prismarine"]=Block(168,0)
mc_items["Prismarine Bricks"]=Block(168,1)
mc_items["Dark Prismarine"]=Block(168,2)
mc_items["Sea Lantern"]=Block(169,0)
mc_items["Hay Bale"]=Block(170,0)
mc_items["White Carpet"]=Block(171,0)
mc_items["Orange Carpet"]=Block(171,1)
mc_items["Magenta Carpet"]=Block(171,2)
mc_items["Light Blue Carpet"]=Block(171,3)
mc_items["Yellow Carpet"]=Block(171,4)
mc_items["Lime Carpet"]=Block(171,5)
mc_items["Pink Carpet"]=Block(171,6)
mc_items["Gray Carpet"]=Block(171,7)
mc_items["Light Gray Carpet"]=Block(171,8)
mc_items["Cyan Carpet"]=Block(171,9)
mc_items["Purple Carpet"]=Block(171,10)
mc_items["Blue Carpet"]=Block(171,11)
mc_items["Brown Carpet"]=Block(171,12)
mc_items["Green Carpet"]=Block(171,13)
mc_items["Red Carpet"]=Block(171,14)
mc_items["Black Carpet"]=Block(171,15)
mc_items["Hardened Clay"]=Block(172,0)
mc_items["Block of Coal"]=Block(173,0)
mc_items["Packed Ice"]=Block(174,0)
mc_items["Sunflower"]=Block(175,0)
mc_items["Lilac"]=Block(175,1)
mc_items["Double Tallgrass"]=Block(175,2)
mc_items["Large Fern"]=Block(175,3)
mc_items["Rose Bush"]=Block(175,4)
mc_items["Peony"]=Block(175,5)
mc_items["Free-standing Banner"]=Block(176,0)
mc_items["Wall-mounted Banner"]=Block(177,0)
mc_items["Inverted Daylight Sensor"]=Block(178,0)
mc_items["Red Sandstone"]=Block(179,0)
mc_items["Chiseled Red Sandstone"]=Block(179,1)
mc_items["Smooth Red Sandstone"]=Block(179,2)
mc_items["Red Sandstone Stairs"]=Block(180,0)
mc_items["Double Red Sandstone Slab"]=Block(181,0)
mc_items["Red Sandstone Slab"]=Block(182,0)
mc_items["Spruce Fence Gate"]=Block(183,0)
mc_items["Birch Fence Gate"]=Block(184,0)
mc_items["Jungle Fence Gate"]=Block(185,0)
mc_items["Dark Oak Fence Gate"]=Block(186,0)
mc_items["Acacia Fence Gate"]=Block(187,0)
mc_items["Spruce Fence"]=Block(188,0)
mc_items["Birch Fence"]=Block(189,0)
mc_items["Jungle Fence"]=Block(190,0)
mc_items["Dark Oak Fence"]=Block(191,0)
mc_items["Acacia Fence"]=Block(192,0)
mc_items["Spruce Door Block"]=Block(193,0)
mc_items["Birch Door Block"]=Block(194,0)
mc_items["Jungle Door Block"]=Block(195,0)
mc_items["Acacia Door Block"]=Block(196,0)
mc_items["Dark Oak Door Block"]=Block(197,0)
mc_items["End Rod"]=Block(198,0)
mc_items["Chorus Plant"]=Block(199,0)
mc_items["Chorus Flower"]=Block(200,0)
mc_items["Purpur Block"]=Block(201,0)
mc_items["Purpur Pillar"]=Block(202,0)
mc_items["Purpur Stairs"]=Block(203,0)
mc_items["Purpur Double Slab"]=Block(204,0)
mc_items["Purpur Slab"]=Block(205,0)
mc_items["End Stone Bricks"]=Block(206,0)
mc_items["Beetroot Block"]=Block(207,0)
mc_items["Grass Path"]=Block(208,0)
mc_items["End Gateway"]=Block(209,0)
mc_items["Repeating Command Block"]=Block(210,0)
mc_items["Chain Command Block"]=Block(211,0)
mc_items["Frosted Ice"]=Block(212,0)
mc_items["Magma Block"]=Block(213,0)
mc_items["Nether Wart Block"]=Block(214,0)
mc_items["Red Nether Brick"]=Block(215,0)
mc_items["Bone Block"]=Block(216,0)
mc_items["Structure Void"]=Block(217,0)
mc_items["Observer"]=Block(218,0)
mc_items["White Shulker Box"]=Block(219,0)
mc_items["Orange Shulker Box"]=Block(220,0)
mc_items["Magenta Shulker Box"]=Block(221,0)
mc_items["Light Blue Shulker Box"]=Block(222,0)
mc_items["Yellow Shulker Box"]=Block(223,0)
mc_items["Lime Shulker Box"]=Block(224,0)
mc_items["Pink Shulker Box"]=Block(225,0)
mc_items["Gray Shulker Box"]=Block(226,0)
mc_items["Light Gray Shulker Box"]=Block(227,0)
mc_items["Cyan Shulker Box"]=Block(228,0)
mc_items["Purple Shulker Box"]=Block(229,0)
mc_items["Blue Shulker Box"]=Block(230,0)
mc_items["Brown Shulker Box"]=Block(231,0)
mc_items["Green Shulker Box"]=Block(232,0)
mc_items["Red Shulker Box"]=Block(233,0)
mc_items["Black Shulker Box"]=Block(234,0)
mc_items["White Glazed Terracotta"]=Block(235,0)
mc_items["Orange Glazed Terracotta"]=Block(236,0)
mc_items["Magenta Glazed Terracotta"]=Block(237,0)
mc_items["Light Blue Glazed Terracotta"]=Block(238,0)
mc_items["Yellow Glazed Terracotta"]=Block(239,0)
mc_items["Lime Glazed Terracotta"]=Block(240,0)
mc_items["Pink Glazed Terracotta"]=Block(241,0)
mc_items["Gray Glazed Terracotta"]=Block(242,0)
mc_items["Light Gray Glazed Terracotta"]=Block(243,0)
mc_items["Cyan Glazed Terracotta"]=Block(244,0)
mc_items["Purple Glazed Terracotta"]=Block(245,0)
mc_items["Blue Glazed Terracotta"]=Block(246,0)
mc_items["Brown Glazed Terracotta"]=Block(247,0)
mc_items["Green Glazed Terracotta"]=Block(248,0)
mc_items["Red Glazed Terracotta"]=Block(249,0)
mc_items["Black Glazed Terracotta"]=Block(250,0)
mc_items["White Concrete"]=Block(251,0)
mc_items["Orange Concrete"]=Block(251,1)
mc_items["Magenta Concrete"]=Block(251,2)
mc_items["Light Blue Concrete"]=Block(251,3)
mc_items["Yellow Concrete"]=Block(251,4)
mc_items["Lime Concrete"]=Block(251,5)
mc_items["Pink Concrete"]=Block(251,6)
mc_items["Gray Concrete"]=Block(251,7)
mc_items["Light Gray Concrete"]=Block(251,8)
mc_items["Cyan Concrete"]=Block(251,9)
mc_items["Purple Concrete"]=Block(251,10)
mc_items["Blue Concrete"]=Block(251,11)
mc_items["Brown Concrete"]=Block(251,12)
mc_items["Green Concrete"]=Block(251,13)
mc_items["Red Concrete"]=Block(251,14)
mc_items["Black Concrete"]=Block(251,15)
mc_items["White Concrete Powder"]=Block(252,0)
mc_items["Orange Concrete Powder"]=Block(252,1)
mc_items["Magenta Concrete Powder"]=Block(252,2)
mc_items["Light Blue Concrete Powder"]=Block(252,3)
mc_items["Yellow Concrete Powder"]=Block(252,4)
mc_items["Lime Concrete Powder"]=Block(252,5)
mc_items["Pink Concrete Powder"]=Block(252,6)
mc_items["Gray Concrete Powder"]=Block(252,7)
mc_items["Light Gray Concrete Powder"]=Block(252,8)
mc_items["Cyan Concrete Powder"]=Block(252,9)
mc_items["Purple Concrete Powder"]=Block(252,10)
mc_items["Blue Concrete Powder"]=Block(252,11)
mc_items["Brown Concrete Powder"]=Block(252,12)
mc_items["Green Concrete Powder"]=Block(252,13)
mc_items["Red Concrete Powder"]=Block(252,14)
mc_items["Black Concrete Powder"]=Block(252,15)
mc_items["Structure Block"]=Block(255,0)
mc_items["Iron Shovel"]=Block(256,0)
mc_items["Iron Pickaxe"]=Block(257,0)
mc_items["Iron Axe"]=Block(258,0)
mc_items["Flint and Steel"]=Block(259,0)
mc_items["Apple"]=Block(260,0)
mc_items["Bow"]=Block(261,0)
mc_items["Arrow"]=Block(262,0)
mc_items["Coal"]=Block(263,0)
mc_items["Charcoal"]=Block(263,1)
mc_items["Diamond"]=Block(264,0)
mc_items["Iron Ingot"]=Block(265,0)
mc_items["Gold Ingot"]=Block(266,0)
mc_items["Iron Sword"]=Block(267,0)
mc_items["Wooden Sword"]=Block(268,0)
mc_items["Wooden Shovel"]=Block(269,0)
mc_items["Wooden Pickaxe"]=Block(270,0)
mc_items["Wooden Axe"]=Block(271,0)
mc_items["Stone Sword"]=Block(272,0)
mc_items["Stone Shovel"]=Block(273,0)
mc_items["Stone Pickaxe"]=Block(274,0)
mc_items["Stone Axe"]=Block(275,0)
mc_items["Diamond Sword"]=Block(276,0)
mc_items["Diamond Shovel"]=Block(277,0)
mc_items["Diamond Pickaxe"]=Block(278,0)
mc_items["Diamond Axe"]=Block(279,0)
mc_items["Stick"]=Block(280,0)
mc_items["Bowl"]=Block(281,0)
mc_items["Mushroom Stew"]=Block(282,0)
mc_items["Golden Sword"]=Block(283,0)
mc_items["Golden Shovel"]=Block(284,0)
mc_items["Golden Pickaxe"]=Block(285,0)
mc_items["Golden Axe"]=Block(286,0)
mc_items["String"]=Block(287,0)
mc_items["Feather"]=Block(288,0)
mc_items["Gunpowder"]=Block(289,0)
mc_items["Wooden Hoe"]=Block(290,0)
mc_items["Stone Hoe"]=Block(291,0)
mc_items["Iron Hoe"]=Block(292,0)
mc_items["Diamond Hoe"]=Block(293,0)
mc_items["Golden Hoe"]=Block(294,0)
mc_items["Wheat Seeds"]=Block(295,0)
mc_items["Wheat"]=Block(296,0)
mc_items["Bread"]=Block(297,0)
mc_items["Leather Helmet"]=Block(298,0)
mc_items["Leather Tunic"]=Block(299,0)
mc_items["Leather Pants"]=Block(300,0)
mc_items["Leather Boots"]=Block(301,0)
mc_items["Chainmail Helmet"]=Block(302,0)
mc_items["Chainmail Chestplate"]=Block(303,0)
mc_items["Chainmail Leggings"]=Block(304,0)
mc_items["Chainmail Boots"]=Block(305,0)
mc_items["Iron Helmet"]=Block(306,0)
mc_items["Iron Chestplate"]=Block(307,0)
mc_items["Iron Leggings"]=Block(308,0)
mc_items["Iron Boots"]=Block(309,0)
mc_items["Diamond Helmet"]=Block(310,0)
mc_items["Diamond Chestplate"]=Block(311,0)
mc_items["Diamond Leggings"]=Block(312,0)
mc_items["Diamond Boots"]=Block(313,0)
mc_items["Golden Helmet"]=Block(314,0)
mc_items["Golden Chestplate"]=Block(315,0)
mc_items["Golden Leggings"]=Block(316,0)
mc_items["Golden Boots"]=Block(317,0)
mc_items["Flint"]=Block(318,0)
mc_items["Raw Porkchop"]=Block(319,0)
mc_items["Cooked Porkchop"]=Block(320,0)
mc_items["Painting"]=Block(321,0)
mc_items["Golden Apple"]=Block(322,0)
mc_items["Enchanted Golden Apple"]=Block(322,1)
mc_items["Sign"]=Block(323,0)
mc_items["Oak Door"]=Block(324,0)
mc_items["Bucket"]=Block(325,0)
mc_items["Water Bucket"]=Block(326,0)
mc_items["Lava Bucket"]=Block(327,0)
mc_items["Minecart"]=Block(328,0)
mc_items["Saddle"]=Block(329,0)
mc_items["Iron Door"]=Block(330,0)
mc_items["Redstone"]=Block(331,0)
mc_items["Snowball"]=Block(332,0)
mc_items["Oak Boat"]=Block(333,0)
mc_items["Leather"]=Block(334,0)
mc_items["Milk Bucket"]=Block(335,0)
mc_items["Brick"]=Block(336,0)
mc_items["Clay"]=Block(337,0)
mc_items["Sugar Canes"]=Block(338,0)
mc_items["Paper"]=Block(339,0)
mc_items["Book"]=Block(340,0)
mc_items["Slimeball"]=Block(341,0)
mc_items["Minecart with Chest"]=Block(342,0)
mc_items["Minecart with Furnace"]=Block(343,0)
mc_items["Egg"]=Block(344,0)
mc_items["Compass"]=Block(345,0)
mc_items["Fishing Rod"]=Block(346,0)
mc_items["Clock"]=Block(347,0)
mc_items["Glowstone Dust"]=Block(348,0)
mc_items["Raw Fish"]=Block(349,0)
mc_items["Raw Salmon"]=Block(349,1)
mc_items["Clownfish"]=Block(349,2)
mc_items["Pufferfish"]=Block(349,3)
mc_items["Cooked Fish"]=Block(350,0)
mc_items["Cooked Salmon"]=Block(350,1)
mc_items["Ink Sack"]=Block(351,0)
mc_items["Rose Red"]=Block(351,1)
mc_items["Cactus Green"]=Block(351,2)
mc_items["Coco Beans"]=Block(351,3)
mc_items["Lapis Lazuli"]=Block(351,4)
mc_items["Purple Dye"]=Block(351,5)
mc_items["Cyan Dye"]=Block(351,6)
mc_items["Light Gray Dye"]=Block(351,7)
mc_items["Gray Dye"]=Block(351,8)
mc_items["Pink Dye"]=Block(351,9)
mc_items["Lime Dye"]=Block(351,10)
mc_items["Dandelion Yellow"]=Block(351,11)
mc_items["Light Blue Dye"]=Block(351,12)
mc_items["Magenta Dye"]=Block(351,13)
mc_items["Orange Dye"]=Block(351,14)
mc_items["Bone Meal"]=Block(351,15)
mc_items["Bone"]=Block(352,0)
mc_items["Sugar"]=Block(353,0)
mc_items["Cake"]=Block(354,0)
mc_items["Bed"]=Block(355,0)
mc_items["Redstone Repeater"]=Block(356,0)
mc_items["Cookie"]=Block(357,0)
mc_items["Map"]=Block(358,0)
mc_items["Shears"]=Block(359,0)
mc_items["Melon"]=Block(360,0)
mc_items["Pumpkin Seeds"]=Block(361,0)
mc_items["Melon Seeds"]=Block(362,0)
mc_items["Raw Beef"]=Block(363,0)
mc_items["Steak"]=Block(364,0)
mc_items["Raw Chicken"]=Block(365,0)
mc_items["Cooked Chicken"]=Block(366,0)
mc_items["Rotten Flesh"]=Block(367,0)
mc_items["Ender Pearl"]=Block(368,0)
mc_items["Blaze Rod"]=Block(369,0)
mc_items["Ghast Tear"]=Block(370,0)
mc_items["Gold Nugget"]=Block(371,0)
mc_items["Nether Wart"]=Block(372,0)
mc_items["Potion"]=Block(373,0)
mc_items["Glass Bottle"]=Block(374,0)
mc_items["Spider Eye"]=Block(375,0)
mc_items["Fermented Spider Eye"]=Block(376,0)
mc_items["Blaze Powder"]=Block(377,0)
mc_items["Magma Cream"]=Block(378,0)
mc_items["Brewing Stand"]=Block(379,0)
mc_items["Cauldron"]=Block(380,0)
mc_items["Eye of Ender"]=Block(381,0)
mc_items["Glistering Melon"]=Block(382,0)
mc_items["Spawn Elder Guardian"]=Block(383,4)
mc_items["Spawn Wither Skeleton"]=Block(383,5)
mc_items["Spawn Stray"]=Block(383,6)
mc_items["Spawn Husk"]=Block(383,23)
mc_items["Spawn Zombie Villager"]=Block(383,27)
mc_items["Spawn Skeleton Horse"]=Block(383,28)
mc_items["Spawn Zombie Horse"]=Block(383,29)
mc_items["Spawn Donkey"]=Block(383,31)
mc_items["Spawn Mule"]=Block(383,32)
mc_items["Spawn Evoker"]=Block(383,34)
mc_items["Spawn Vex"]=Block(383,35)
mc_items["Spawn Vindicator"]=Block(383,36)
mc_items["Spawn Creeper"]=Block(383,50)
mc_items["Spawn Skeleton"]=Block(383,51)
mc_items["Spawn Spider"]=Block(383,52)
mc_items["Spawn Zombie"]=Block(383,54)
mc_items["Spawn Slime"]=Block(383,55)
mc_items["Spawn Ghast"]=Block(383,56)
mc_items["Spawn Zombie Pigman"]=Block(383,57)
mc_items["Spawn Enderman"]=Block(383,58)
mc_items["Spawn Cave Spider"]=Block(383,59)
mc_items["Spawn Silverfish"]=Block(383,60)
mc_items["Spawn Blaze"]=Block(383,61)
mc_items["Spawn Magma Cube"]=Block(383,62)
mc_items["Spawn Bat"]=Block(383,65)
mc_items["Spawn Witch"]=Block(383,66)
mc_items["Spawn Endermite"]=Block(383,67)
mc_items["Spawn Guardian"]=Block(383,68)
mc_items["Spawn Shulker"]=Block(383,69)
mc_items["Spawn Pig"]=Block(383,90)
mc_items["Spawn Sheep"]=Block(383,91)
mc_items["Spawn Cow"]=Block(383,92)
mc_items["Spawn Chicken"]=Block(383,93)
mc_items["Spawn Squid"]=Block(383,94)
mc_items["Spawn Wolf"]=Block(383,95)
mc_items["Spawn Mooshroom"]=Block(383,96)
mc_items["Spawn Ocelot"]=Block(383,98)
mc_items["Spawn Horse"]=Block(383,100)
mc_items["Spawn Rabbit"]=Block(383,101)
mc_items["Spawn Polar Bear"]=Block(383,102)
mc_items["Spawn Llama"]=Block(383,103)
mc_items["Spawn Villager"]=Block(383,120)
mc_items["Bottle o' Enchanting"]=Block(384,0)
mc_items["Fire Charge"]=Block(385,0)
mc_items["Book and Quill"]=Block(386,0)
mc_items["Written Book"]=Block(387,0)
mc_items["Emerald"]=Block(388,0)
mc_items["Item Frame"]=Block(389,0)
mc_items["Flower Pot"]=Block(390,0)
mc_items["Carrot"]=Block(391,0)
mc_items["Potato"]=Block(392,0)
mc_items["Baked Potato"]=Block(393,0)
mc_items["Poisonous Potato"]=Block(394,0)
mc_items["Empty Map"]=Block(395,0)
mc_items["Golden Carrot"]=Block(396,0)
mc_items["Mob Head (Skeleton)"]=Block(397,0)
mc_items["Mob Head (Wither Skeleton)"]=Block(397,1)
mc_items["Mob Head (Zombie)"]=Block(397,2)
mc_items["Mob Head (Human)"]=Block(397,3)
mc_items["Mob Head (Creeper)"]=Block(397,4)
mc_items["Mob Head (Dragon)"]=Block(397,5)
mc_items["Carrot on a Stick"]=Block(398,0)
mc_items["Nether Star"]=Block(399,0)
mc_items["Pumpkin Pie"]=Block(400,0)
mc_items["Firework Rocket"]=Block(401,0)
mc_items["Firework Star"]=Block(402,0)
mc_items["Enchanted Book"]=Block(403,0)
mc_items["Redstone Comparator"]=Block(404,0)
mc_items["Nether Brick"]=Block(405,0)
mc_items["Nether Quartz"]=Block(406,0)
mc_items["Minecart with TNT"]=Block(407,0)
mc_items["Minecart with Hopper"]=Block(408,0)
mc_items["Prismarine Shard"]=Block(409,0)
mc_items["Prismarine Crystals"]=Block(410,0)
mc_items["Raw Rabbit"]=Block(411,0)
mc_items["Cooked Rabbit"]=Block(412,0)
mc_items["Rabbit Stew"]=Block(413,0)
mc_items["Rabbit's Foot"]=Block(414,0)
mc_items["Rabbit Hide"]=Block(415,0)
mc_items["Armor Stand"]=Block(416,0)
mc_items["Iron Horse Armor"]=Block(417,0)
mc_items["Golden Horse Armor"]=Block(418,0)
mc_items["Diamond Horse Armor"]=Block(419,0)
mc_items["Lead"]=Block(420,0)
mc_items["Name Tag"]=Block(421,0)
mc_items["Minecart with Command Block"]=Block(422,0)
mc_items["Raw Mutton"]=Block(423,0)
mc_items["Cooked Mutton"]=Block(424,0)
mc_items["Banner"]=Block(425,0)
mc_items["Spruce Door"]=Block(427,0)
mc_items["Birch Door"]=Block(428,0)
mc_items["Jungle Door"]=Block(429,0)
mc_items["Acacia Door"]=Block(430,0)
mc_items["Dark Oak Door"]=Block(431,0)
mc_items["Chorus Fruit"]=Block(432,0)
mc_items["Popped Chorus Fruit"]=Block(433,0)
mc_items["Beetroot"]=Block(434,0)
mc_items["Beetroot Seeds"]=Block(435,0)
mc_items["Beetroot Soup"]=Block(436,0)
mc_items["Dragon's Breath"]=Block(437,0)
mc_items["Splash Potion"]=Block(438,0)
mc_items["Spectral Arrow"]=Block(439,0)
mc_items["Tipped Arrow"]=Block(440,0)
mc_items["Lingering Potion"]=Block(441,0)
mc_items["Shield"]=Block(442,0)
mc_items["Elytra"]=Block(443,0)
mc_items["Spruce Boat"]=Block(444,0)
mc_items["Birch Boat"]=Block(445,0)
mc_items["Jungle Boat"]=Block(446,0)
mc_items["Acacia Boat"]=Block(447,0)
mc_items["Dark Oak Boat"]=Block(448,0)
mc_items["Totem of Undying"]=Block(449,0)
mc_items["Shulker Shell"]=Block(450,0)
mc_items["Iron Nugget"]=Block(452,0)
mc_items["13 Disc"]=Block(2256,0)
mc_items["Cat Disc"]=Block(2257,0)
mc_items["Blocks Disc"]=Block(2258,0)
mc_items["Chirp Disc"]=Block(2259,0)
mc_items["Far Disc"]=Block(2260,0)
mc_items["Mall Disc"]=Block(2261,0)
mc_items["Mellohi Disc"]=Block(2262,0)
mc_items["Stal Disc"]=Block(2263,0)
mc_items["Strad Disc"]=Block(2264,0)
mc_items["Ward Disc"]=Block(2265,0)
mc_items["11 Disc"]=Block(2266,0)
mc_items["Wait Disc"]=Block(2267,0)

find_item(name::String) = mc_items[name]
find_item(id::Int, data::Int=0) = Block(id, data)


text:
function partition!(A, p, r)
	x = A[r]
	i = p-1
	for j in p:(r-1)
	    if A[j] <= x
	        i += 1
	        tmp = A[i]
	        A[i] = A[j]
	        A[j] = tmp
	    end
	end
	tmp = A[r]
	A[r] = A[i+1]
	A[i+1] = tmp
	return i+1
end

function quick_sort!(A, p, r)
    if p < r
        q = partition!(A, p, r)
        quick_sort!(A, p, q-1)
        quick_sort!(A, q+1, r)
    end
    return A
end

function algdat_sort!(A)
    return quick_sort!(A, 1, length(A))
end

function bisect_left(A, p, r, v)
    i = p
    if p < r
        q = floor(Int, (p+r)/2)
        if v <= A[q]
            i = bisect_left(A, p, q, v)
        else
            i = bisect_left(A, q+1, r, v)
        end
    end
    return i
end

function bisect_right(A, p, r, v)
    i = p
    if p < r
        q = floor(Int, (p+r)/2)
        if v < A[q]
            i = bisect_right(A, p, q, v)
       	else
            i = bisect_right(A, q+1, r, v)
        end
    end
    return i
end

function find_median(A, lower, upper)
    min_index = bisect_left(A, 1, length(A)+1, lower)
    max_index = bisect_right(A, 1, length(A)+1, upper) - 1

    if (max_index-min_index+1)%2 == 0
        i = floor(Int, (max_index-min_index)/2)
        i += min_index
        return (A[i] + A[i+1])/2
    else
        return A[min_index + floor(Int, (max_index - min_index)/2)]
    end
end

ext: jl


text:
include("bson_tests.jl")
include("mongodb_tests.jl")

if !Sys.iswindows()
	include("replica_set_tests.jl")
end

Mongoc.mongoc_cleanup()


text:
@testset "test cases where some connections have less than 3 conductors" begin

    @testset "single_conductor_network_matpower" begin

        model = _PMD.ACPUPowerModel
        msr_path = joinpath(mktempdir(),"temp.csv")
        pm_path = joinpath(dirname(pathof(PowerModels)), "..")
        case5 = PowerModels.parse_file("$(pm_path)/test/data/matpower/case5.m"); _PMD.make_multiconductor!(case5, 1)

        # case5 below is simplified for ease of debugging and because the data contains indications that lead to
        # sophisticated power flow calculations, whose results cannot be translated into a digestible state estimation
        # input with the current default measurement parser

        for (g, gen) in case5["gen"]
            if g ∉ ["4"] #this is the generator at the reference bus. allow just one generator to avoid the `bus = 2` inconvenient, at the moment
                delete!(case5["gen"], g)
            else
                gen["pmax"] = [Inf]
                gen["qmax"] = [Inf]
                gen["pmin"] = [-Inf]
                gen["qmin"] = [-Inf]
            end
        end

        for (b, bus) in case5["bus"]
            if bus["bus_type"] == 2
                bus["bus_type"] = 1
            end
            bus["vmin"] = [0.0]
            bus["vmax"] = [Inf]
        end

        for (b, branch) in case5["branch"]
            branch["rate_a"] = branch["rate_b"] = branch["rate_c"] = [Inf]
            if b ∈ ["5", "6"]
                delete!(case5["branch"], b)
            end
        end

        pf_result = _PMD.solve_mc_pf(case5, model, ipopt_solver)
        _PMDSE.write_measurements!(_PMD.ACPUPowerModel, case5, pf_result, msr_path, exclude = ["vi","vr"])
        _PMDSE.add_measurements!(case5, msr_path, actual_meas = true)
        _PMDSE.add_voltage_measurement!(case5, pf_result, 0.0005)

        case5["se_settings"] = Dict{String,Any}("criterion" => "rwlav", "rescaler" => 1)

        # solve the state estimation
        se_result = _PMDSE.solve_mc_se(case5, model, ipopt_solver)

        @test pf_result["termination_status"] == LOCALLY_SOLVED
        @test se_result["termination_status"] == LOCALLY_SOLVED
        @test isapprox(se_result["objective"], 0.0; atol = 2e-7)

    end #testset

    @testset "three-phase_network_reduced" begin

        data = _PMD.parse_file(joinpath(BASE_DIR, "test/data/extra/networks/case3_unbalanced.dss"); data_model=MATHEMATICAL)
        delete!(data["load"], "2")
        delete!(data["load"], "3")
        reduce_single_phase_loadbuses!(data, exclude = []) #after this, all buses have 3 terminals except bus 3. All branches have 3 connections except branch 1. Thus, dimensions are reduced.
        pf_result= _PMD.solve_mc_pf(data, _PMD.ACPUPowerModel, ipopt_solver)
        msr_path = joinpath(mktempdir(),"temp.csv")
        _PMDSE.write_measurements!(_PMD.ACPUPowerModel, data, pf_result, msr_path, exclude = ["vr","vi"])
        _PMDSE.add_measurements!(data, msr_path, actual_meas = true)
        data["se_settings"] = Dict{String,Any}("criterion" => "rwlav",
                                "rescaler" => 1)
        se_result = _PMDSE.solve_mc_se(data, _PMD.ACPUPowerModel, ipopt_solver)
        delta, max, avg = _PMDSE.calculate_voltage_magnitude_error(se_result, pf_result)
        @test isapprox(max, 0.0; atol = 1e-6)
        @test isapprox(avg, 0.0; atol = 1e-8)

    end

end


text:
sigmoid(x) = 1 ./ (1 .+ e.^(-x))

function ∇sigmoid(x)
    f = sigmoid(x)
    return f .* (1 - f)
end

max_stars_repo_name: IshitaTakeshi/NeuralNetwork

max_issues_repo_name: IshitaTakeshi/NeuralNetwork

max_forks_repo_name: IshitaTakeshi/NeuralNetwork


text:
using RoME, IncrementalInference

# using CloudGraphs, Neo4j
# include("BlandAuthDB.jl")
#
# configuration = CloudGraphs.CloudGraphConfiguration(dbaddress, 7474, dbusr, dbpwd, mongoaddress, 27017, false, "", "");
# cloudGraph = connect(configuration);
# # register types of interest in CloudGraphs
# registerGeneralVariableTypes!(cloudGraph)
# IncrementalInference.setCloudDataLayerAPI!()

# togglePrtStbLines()

N = 300
fg = initfg()
fg.sessionname="SESSranges"
# fg.cg = cloudGraph

# initCov = diagm([0.03;0.03;0.001])
odoCov = diagm([3.0;3.0;0.1])

# Some starting position
init = 300*randn(2,N)
v1 = addNode!(fg, "l1", init, diagm([1000.0;1000.0]), N=N, ready=0)

# Two landmarks
L1, L2, L3 = [10.0;30], [30.0;-30], [70.0;30]
l1 = addNode!(fg, "l2", reshape(L1,length(L1),1), diagm([1.0;1.0]), N=N, ready=0)
l2 = addNode!(fg, "l3", reshape(L2,length(L2),1), diagm([1.0;1.0]), N=N, ready=0)

# must pin landmarks for guage
pp2 = PriorPoint2D(L1, diagm([1.0;1.0]), [1.0])
f = addFactor!(fg,[l1], pp2)
pp2 = PriorPoint2D(L2, diagm([1.0;1.0]), [1.0])
f = addFactor!(fg, [l2], pp2)

# and constraints to pose x1
P1 = [0.0;0]
rhoZ1 = norm(L1-P1)
ppr = Point2DPoint2DRange([rhoZ1], 2.0, [1.0])
addFactor!(fg, [v1;l1], ppr, ready=0)

rhoZ2 = norm(L2-P1)
ppr = Point2DPoint2DRange([rhoZ2], 3.0, [1.0])
addFactor!(fg, [v1;l2], ppr, ready=0)

#solve
writeGraphPdf(fg)
tree = prepBatchTree!(fg, drawpdf=true)

inferOverTree!(fg, tree, N=N)

drawLandms(fg,showmm=true)

drawLandms(fg,showmm=true,from=1,to=1)
drawLandms(fg,showmm=true,from=4)

# setDBAllReady!(fg)

#pose 2 in truth is at
P2 = [50.0;0.0]

# drive forward 50 units
# v2 = addOdoFG!(fg, "l4", [50.0;0.0;0.0], odoCov, ready=0, N=N)
ppr = Point2DPoint2DRange([norm(P1-P2)], 3.0, [1.0])
v2 = addNode!(fg, "l4", init, diagm([3.0;3.0]), N=N, ready=0)
addFactor!(fg, [v1;v2], ppr, ready=0)

# solve again
writeGraphPdf(fg)
tree = wipeBuildNewTree!(fg)

inferOverTree!(fg, tree, N=N)

drawLandms(fg,showmm=true)

drawLandms(fg,showmm=true,from=4)

# get one range to second position
rhoZ3 = norm(L2-P2)
ppr = Point2DPoint2DRange([rhoZ3], 3.0, [1.0])
addFactor!(fg, [v2;l2], ppr, ready=0)

# solve again
writeGraphPdf(fg)
tree = wipeBuildNewTree!(fg)

inferOverTree!(fg, tree, N=N)

drawLandms(fg,showmm=true)

drawLandms(fg,showmm=true,from=4)

# setDBAllReady!(fg)

# get another range
rhoZ4 = norm(L1-P2)
ppr = Point2DPoint2DRange([rhoZ4], 3.0, [1.0])
addFactor!(fg, [v2;l1], ppr, ready=0)

# solve again
writeGraphPdf(fg)
tree = wipeBuildNewTree!(fg, drawpdf=true)

inferOverTree!(fg, tree, N=N)

drawLandms(fg,showmm=true)

# START seeing a new range signal
ppr = Point2DPoint2DRange([norm(P2-L3)], 3.0, [1.0])
l3 = addNode!(fg, "l5", init, diagm([3.0;3.0]), N=N, ready=0)
addFactor!(fg, [v2;l3], ppr, ready=0)

#pose 3 in truth is at
P3 = [100.0;0.0]

# drive forward 50 units
# v2 = addOdoFG!(fg, "l4", [50.0;0.0;0.0], odoCov, ready=0, N=N)
ppr = Point2DPoint2DRange([norm(P2-P3)], 3.0, [1.0])
v3 = addNode!(fg, "l6", init, diagm([3.0;3.0]), N=N, ready=0)
addFactor!(fg, [v2;v3], ppr, ready=0)

# solve again
writeGraphPdf(fg)
tree = wipeBuildNewTree!(fg)

inferOverTree!(fg, tree, N=N)

drawLandms(fg,showmm=true)

drawLandms(fg,showmm=true,from=5)

# get one range to third position
rhoZ5 = norm(L2-P3)
ppr = Point2DPoint2DRange([rhoZ5], 3.0, [1.0])
addFactor!(fg, [v3;l2], ppr, ready=0)

# solve again
# writeGraphPdf(fg)
# tree = wipeBuildNewTree!(fg)

# inferOverTree!(fg, tree, N=N)
#
# drawLandms(fg,showmm=true)
#
# drawLandms(fg,showmm=true,from=5)

rhoZ6 = norm(L1-P3)
ppr = Point2DPoint2DRange([rhoZ6], 3.0, [1.0])
addFactor!(fg, [v3;l1], ppr, ready=0)

rhoZ6b = norm(L3-P3)
ppr = Point2DPoint2DRange([rhoZ6b], 3.0, [1.0])
addFactor!(fg, [v3;l3], ppr, ready=0)

# solve again
writeGraphPdf(fg)
tree = wipeBuildNewTree!(fg)

inferOverTree!(fg, tree, N=N)

drawLandms(fg,showmm=true)

drawLandms(fg,showmm=true,from=5,to=5)

#pose 4 in truth is at
P4 = [100.0;70.0]

# drive forward 50 units
# v2 = addOdoFG!(fg, "l4", [50.0;0.0;0.0], odoCov, ready=0, N=N)
ppr = Point2DPoint2DRange([norm(P3-P4)], 3.0, [1.0])
v4 = addNode!(fg, "l7", init, diagm([3.0;3.0]), N=N, ready=0)
addFactor!(fg, [v3;v4], ppr, ready=0)

# solve again
writeGraphPdf(fg)
tree = wipeBuildNewTree!(fg)

inferOverTree!(fg, tree, N=N)

drawLandms(fg,showmm=true)

drawLandms(fg,showmm=true,from=5)

# get one range to fourth position
rhoZ7 = norm(L2-P4)
ppr = Point2DPoint2DRange([rhoZ7], 3.0, [1.0])
addFactor!(fg, [v4;l2], ppr, ready=0)

# solve again
writeGraphPdf(fg)
tree = wipeBuildNewTree!(fg)

inferOverTree!(fg, tree, N=N)

drawLandms(fg,showmm=true)

drawLandms(fg,showmm=true,from=4)

# get one range to fourth position
rhoZ8 = norm(L1-P4)
ppr = Point2DPoint2DRange([rhoZ8], 3.0, [1.0])
addFactor!(fg, [v4;l1], ppr, ready=0)

# solve again
writeGraphPdf(fg)
tree = wipeBuildNewTree!(fg)

inferOverTree!(fg, tree, N=N)

drawLandms(fg,showmm=true)

drawLandms(fg,showmm=true,from=6)

# get one range to fourth position
rhoZ9 = norm(L3-P4)
ppr = Point2DPoint2DRange([rhoZ9], 3.0, [1.0])
addFactor!(fg, [v4;l3], ppr, ready=0)

# solve again
writeGraphPdf(fg)
tree = wipeBuildNewTree!(fg)

inferOverTree!(fg, tree, N=N)

drawLandms(fg,showmm=true)

drawLandms(fg,showmm=true,from=5,to=5)

#pose 5 in truth is at
P5 = [150.0;70.0]

# drive forward 50 units
# v2 = addOdoFG!(fg, "l4", [50.0;0.0;0.0], odoCov, ready=0, N=N)
ppr = Point2DPoint2DRange([norm(P4-P5)], 3.0, [1.0])
v5 = addNode!(fg, "l8", init, diagm([3.0;3.0]), N=N, ready=0)
addFactor!(fg, [v4;v5], ppr, ready=0)

rhoZ10 = norm(L3-P5)
ppr = Point2DPoint2DRange([rhoZ10], 3.0, [1.0])
addFactor!(fg, [v5;l3], ppr, ready=0)

rhoZ11 = norm(L2-P5)
ppr = Point2DPoint2DRange([rhoZ11], 3.0, [1.0])
addFactor!(fg, [v5;l2], ppr, ready=0)

# solve again
writeGraphPdf(fg)
tree = wipeBuildNewTree!(fg)

inferOverTree!(fg, tree, N=N)

drawLandms(fg,showmm=true)

rhoZ12 = norm(L1-P5)
ppr = Point2DPoint2DRange([rhoZ12], 3.0, [1.0])
addFactor!(fg, [v5;l1], ppr, ready=0)

# solve again
writeGraphPdf(fg)
tree = wipeBuildNewTree!(fg)

inferOverTree!(fg, tree, N=N)

drawLandms(fg,showmm=true)

#


text:
using Test

# This function takes
#     - v: value in register
#     - a: a  scaling value for the logarithm based on Morris's paper
# It returns n(v,a), the approximate count
function n(v, a)
    a*((1+1/a)^v-1)
end

# This function takes
#    - v: value in register
#    - a: a scaling value for the logarithm based on Morris's paper
# It returns a new value for v
function increment(v, a)
    # delta is the probability of incrementing our counter
    delta = 1/(n(v+1, a)-n(v, a))

    if rand() <= delta
        return v += 1
    else
        return v
    end
end

# This simulates counting and takes
#     - n_items: number of items to count and loop over
#     - a: a scaling value for the logarithm based on Morris's paper
# It returns n(v,a), the approximate count
function approximate_count(n_items, a)
    v = 0
    for i = 1:n_items
        v = increment(v, a)
    end

    return n(v, a)
end

# This function takes
#     - n_trials: the number of counting trials
#     - n_items: the number of items to count to
#     - a: a scaling value for the logarithm based on Morris's paper
#     - threshold: the maximum percent error allowed
# It returns a true / false test value
function test_approximate_count(n_trials, n_items, a, threshold)
    samples = [approximate_count(n_items, a) for i = 1:n_trials]

    avg = sum(samples)/n_trials

    @test ((avg - n_items) / n_items < threshold)
end

@testset "Counting Tests, 100 trials" begin
    println("testing 1,000, a = 30, 1% error")
        test_approximate_count(100, 1000, 30, 0.1)
    println("testing 12,345, a = 10, 1% error")
        test_approximate_count(100, 12345, 10, 0.1)
    # Note: with a lower a, we need more trials, so a higher % error here.
    println("testing 222,222, a = 0.5, 10% error")
        test_approximate_count(100, 222222, 0.5, 0.2)
end


text:
import JSON

struct INGCategoryMatchers # PCRE regex
    countername::String
    counteraccount::String
    description::String
end

struct INGCategoryMatchRule
    category::String
    matchers::INGCategoryMatchers
end

struct INGConfig
    accountnames::Array{String}
    netignoreaccounts::Array{String}
    categorymatchrules::Array{INGCategoryMatchRule}
end

function parse_ing_config(text::String)::INGConfig
    json = JSON.parse(text)
    return INGConfig(
        json["accountnames"],
        json["netignoreaccounts"],
        map(mr -> INGCategoryMatchRule(
            mr[1],
            INGCategoryMatchers(
                get(mr[2], "countername", ""),
                get(mr[2], "counteraccount", ""),
                get(mr[2], "description", "")
            )
        ), json["categorymatchrules"])
    )
end

function load_ing_config_from_file(path::String)::INGConfig
    open(path, "r") do f
        return parse_ing_config(join(eachline(f), "\n"))
    end
end

max_stars_repo_name: CrazedProgrammer/BalancePlot

max_issues_repo_name: CrazedProgrammer/BalancePlot

max_forks_repo_name: CrazedProgrammer/BalancePlot


text:
using Test
using ParametricInversion

function test(x, y, z)
  # Test
  @test choose(+, Tuple{Int, Int}, Z, cXY((x, 2)), nothing) == x + y
  @test sum(choose(+, Tuple{Int, Int}, XY, cZ((z)), nothing)) == z
end

@testset begin "choose"
  test(3,4,5)
end


text:
using Test, CommonTests

@common @testset "greet test" begin
    @test occursin("this is $TERGET.", greet(TERGET))
end


text:
"""
`CompositeDict` is the abstract supertype of a dictionary that consists of
several subdictionaries. The `CompositeDict` type defines common routines for
indexing and iteration.

The representation of a `CompositeDict` is a `BlockVector`. The outer array of this
`BlockVector` adopts the structure of the components of the `CompositeDict`: if the components
are stored in a tuple, the outer array will be a tuple. If the components are
stored in an array, the outer array will be an array as well.

CompositeDict's that store components in an array can have a large number of components.
However, for good efficiency they should have the same type. Tuples are suitable
to hold mixed-type components with good efficiency, however in this case there can't
be too many.

The concrete subtypes differ in what evaluation means. Examples include:
- MultiDict, where evaluation is the sum of the evaluation of the subsets
- PiecewiseDict, where evaluation depends on the location of the evaluation point
"""
abstract type CompositeDict{S,T} <: Dictionary{S,T}
end

# We assume that every subset has an indexable field called dicts
components(set::CompositeDict) = set.dicts

# component(set::CompositeDict, range::AbstractRange) = similardictionary(set, set.dicts[range])
component(set::CompositeDict, range::AbstractRange) = error("this method is deprecated")

tail(set::CompositeDict) = ncomponents(set) == 2 ? component(set, 2) : similardictionary(set, component(set, 2:ncomponents(set)))

# We compute offsets of the individual dicts using a cumulative sum
compute_offsets(dicts::Array) = [0; cumsum(map(length, dicts))]

# Convert a tuple to an array in order to use cumsum like above
compute_offsets(dicts::NTuple{N,Any}) where {N} = compute_offsets([dict for dict in dicts])

"""
Pass the internal `offsets` vector of the composite dict. This is not safe because
the vector is not copied, hence its components could be changed. That would affect
the original composite dictionary. Use with care.
"""
unsafe_offsets(dict::CompositeDict) = dict.offsets

# Implement equality in terms of equality of the components.
==(s1::CompositeDict, s2::CompositeDict) = (components(s1) == components(s2))

size(s::CompositeDict) = (s.offsets[end],)

dimensions(d::CompositeDict) = map(dimensions, components(d))

## Concrete subtypes should override similardictionary and call their own constructor

similar(d::CompositeDict, ::Type{T}, n::Int) where {T} = similar(d, T, composite_size(d, n))

function similar(d::CompositeDict, ::Type{T}, size::Vector{Int}) where {T}
    @assert ncomponents(d) == length(size)
    similardictionary(d, map( (s,l) -> similar(s, T, l), components(d), size))
end

function similar(d::CompositeDict, ::Type{T}, size::Int...) where {T}
    @assert ncomponents(d) == length(size)
    similardictionary(d, map( (s,l) -> similar(s, T, l), components(d), size))
end

resize(d::CompositeDict, size) = similardictionary(d, map(resize, components(d), size))

composite_length(d::CompositeDict) = tuple(map(length, components(d))...)
block_length(dict::CompositeDict) = composite_length(dict)

function composite_size(d::CompositeDict, n::Int)
    if n == length(d)
        map(length, components(d))
    else
        L = ceil(Int, n/ncomponents(d))
        @assert ncomponents(d) * L == n
        L * ones(Int, ncomponents(d))
    end
end

function zeros(::Type{T}, set::CompositeDict) where {T}
    Z = BlockArray{T}(undef,[length(e) for e in components(set)])
    fill!(Z, 0)
    Z
end

tocoefficientformat(a, d::CompositeDict) = BlockVector(a, [length(e) for e in components(set)])

for op in (:isreal, )
    @eval $op(set::CompositeDict) = reduce($op, components(set))
end

for op in (:hasderivative, :hasantiderivative, :hasextension)
    @eval $op(set::CompositeDict) = mapreduce($op, &, components(set))
end
hasderivative(Φ::CompositeDict, order) =
    mapreduce(hasderivative, &, components(Φ))
hasantiderivative(Φ::CompositeDict, order) =
    mapreduce(hasantiderivative, &, components(Φ))

coefficienttype(dict::CompositeDict) = coefficienttype(component(dict,1))

##################
# Indexing
##################

native_index(dict::CompositeDict, idx::MultilinearIndex) = idx

multilinear_index(dict::CompositeDict, idx::LinearIndex) =
    offsets_multilinear_index(unsafe_offsets(dict), idx)

linear_index(dict::CompositeDict, idx::MultilinearIndex) =
    offsets_linear_index(unsafe_offsets(dict), idx)

# The native index is a MultilinearIndex, defined in basis/dictionary/indexing.jl
ordering(d::CompositeDict) = MultilinearIndexList(unsafe_offsets(d))

# # We have to amend the boundscheck ecosystem to catch some cases:
# # - This line will catch indexing with tuples of integers, and we assume
# #   the user wanted to use a CartesianIndex
# checkbounds(::Type{Bool}, d::CompositeDict, idx::MultilinearIndices) =
#     checkbounds(Bool, d, (outerindex(idx), linear_index(component(d,innerindex(idx)), outerindex(idx))))
# # - and this line to avoid an ambiguity
# checkbounds(::Type{Bool}, d::CompositeDict, idx::Tuple{Int,Int}) =
#     checkbounds(Bool, d, linear_index(d, idx))

# For getindex: return indexed basis function of the underlying set
getindex(dict::CompositeDict, idx::LinearIndex) = getindex(dict, native_index(dict, idx))
getindex(dict::CompositeDict, idx::MultilinearIndices) = dict.dicts[outerindex(idx)][innerindex(idx)]

dict_in_support(set::CompositeDict, idx, x) = _dict_in_support(set, components(set), idx, x)
_dict_in_support(set::CompositeDict, dicts, idx, x) = in_support(dicts[outerindex(idx)], innerindex(idx), x)

eachindex(set::CompositeDict) = MultilinearIndexIterator(map(length, components(set)))

## Extension and restriction

extensionsize(set::CompositeDict) = map(extensionsize, components(set))

for op in [:extension, :restriction]
    @eval $op(::Type{T}, src::CompositeDict, dest::CompositeDict; options...) where {T} =
        BlockDiagonalOperator( DictionaryOperator{T}[$op(component(src,i),component(dest,i); options...) for i in 1:ncomponents(src)], src, dest)
end

# Calling and evaluation
unsafe_eval_element(set::CompositeDict, idx::Int, x) = unsafe_eval_element(set, multilinear_index(set,idx), x)

unsafe_eval_element(set::CompositeDict{S,T}, idx::MultilinearIndices, x) where {S,T} =
    convert(T, unsafe_eval_element( component(set, outerindex(idx)), innerindex(idx), x))

unsafe_eval_element_derivative(set::CompositeDict, idx::Int, x, order) = unsafe_eval_element_derivative(set, multilinear_index(set,idx), x, order)

unsafe_eval_element_derivative(set::CompositeDict{S,T}, idx::MultilinearIndices, x, order) where {S,T} =
    convert(T, unsafe_eval_element_derivative( component(set, outerindex(idx)), innerindex(idx), x, order))

derivative_dict(s::CompositeDict, order; options...) =
    similardictionary(s,map(u->derivative_dict(u, order; options...), components(s)))

antiderivative_dict(s::CompositeDict, order; options...) =
    similardictionary(s,map(u->antiderivative_dict(u, order; options...), components(s)))

function evaluation_matrix(::Type{T}, dict::CompositeDict, pts) where {T}
    a = BlockArray{T}(undef, [length(pts),], collect(composite_length(dict)))
    evaluation_matrix!(a, dict, pts)
end

innerproduct1(d1::CompositeDict, i, d2, j, measure; options...) =
    innerproduct(component(d1, outerindex(i)), innerindex(i), d2, j, measure; options...)
innerproduct2(d1, i, d2::CompositeDict, j, measure; options...) =
    innerproduct(d1, i, component(d2, outerindex(j)), innerindex(j), measure; options...)

## Printing

Display.displaystencil(d::CompositeDict) = composite_displaystencil(d)
show(io::IO, mime::MIME"text/plain", d::CompositeDict) = composite_show(io, mime, d)
show(io::IO, d::CompositeDict) = composite_show_compact(io, d)


text:
"""
    summarize(source::Data.Table, f::Formula)
    summarize!(sch::Data.Schema, source::Data.Table, f::Formula)

Compute summaries of the data from source as necessary to evaluate a formula.
Summaries are stored in the metadata of the schema for source.  This is mediated
by dispatching on the types of the terms and the types of the data via
`make_summarizer`.
"""
summarize(source::Data.Table, fs::Formula...) = summarize!(Data.schema(source), source, fs...)

function summarize!(sch::Data.Schema, source::Data.Table, fs::Formula...)
    terms = mapreduce(f->f.term.rhs, vcat, [], fs)
    types = Dict(Symbol(k) => Data.types(sch)[sch[k]] for k in Data.header(sch))
    summarizers = unique(reduce(vcat, [], make_summarizer(t, types) for t in terms))
    isempty(summarizers) && return sch
    for nt in Data.rows(source)
        for s in summarizers
            update!(s, nt)
        end
    end
    summaries = Dict()
    for s in summarizers
        push!(get!(Dict, summaries, name(s)), get(s))
    end
    sch.metadata[:summaries] = summaries
    return sch
end


text:
using ADCME
using PyCall
using LinearAlgebra
using PyPlot
using Random
Random.seed!(233)

chol_orth_op = load_op_and_grad("./build/libCholOrthOp", "chol_orth_op")
h0 = rand(3,3)
h0 = h0'*h0
ny = 4
y = rand(2, ny)

yi = y[1,:]
L = [yi[1] 0.0 0.0;yi[2] yi[3] 0.0; 0.0 0.0 yi[4]]
out1 = L * L'
yi = y[2,:]
L =  [yi[1] 0.0 0.0;yi[2] yi[3] 0.0; 0.0 0.0 yi[4]]
out2 = L * L'

# TODO: specify your input parameters
u = chol_orth_op(constant(y))
sess = Session()
init(sess)
@show run(sess, u)[1,:]-out1[:]
@show run(sess, u)[2,:]-out2[:]
# error()

# TODO: change your test parameter to `m`
# gradient check -- v
function scalar_function(m)
    return sum(chol_orth_op(m))
end

# TODO: change `m_` and `v_` to appropriate values
m_ = constant(rand(10,ny))
v_ = rand(10,ny)
y_ = scalar_function(m_)
dy_ = gradients(y_, m_)
ms_ = Array{Any}(undef, 5)
ys_ = Array{Any}(undef, 5)
s_ = Array{Any}(undef, 5)
w_ = Array{Any}(undef, 5)
gs_ =  @. 1 / 10^(1:5)

for i = 1:5
    g_ = gs_[i]
    ms_[i] = m_ + g_*v_
    ys_[i] = scalar_function(ms_[i])
    s_[i] = ys_[i] - y_
    w_[i] = s_[i] - g_*sum(v_.*dy_)
end

sess = Session()
init(sess)
sval_ = run(sess, s_)
wval_ = run(sess, w_)
close("all")
loglog(gs_, abs.(sval_), "*-", label="finite difference")
loglog(gs_, abs.(wval_), "+-", label="automatic differentiation")
loglog(gs_, gs_.^2 * 0.5*abs(wval_[1])/gs_[1]^2, "--",label="\$\\mathcal{O}(\\gamma^2)\$")
loglog(gs_, gs_ * 0.5*abs(sval_[1])/gs_[1], "--",label="\$\\mathcal{O}(\\gamma)\$")

plt.gca().invert_xaxis()
legend()
xlabel("\$\\gamma\$")
ylabel("Error")


text:
module MuskingumRouting

function rout(inflow, k, x, dt)
    C0,C1,C2 = calc_cs(k,x,dt)

    len = length(inflow)
    outflow = zeros(Float64,len)
    outflow[1] = inflow[1]

    for i in 1:(len-1)
        ni = inflow[i+1]
        ti = inflow[i]
        to = outflow[i]

        outflow[i+1] = (C0 * ni) + (C1 * ti) + (C2 * to)
    end

    return outflow
end

function calc_cs(k, x, dt)
    Δt = (0.5 * dt)
    invx = k * (1 - x)
    C0 = ((-k * x) + Δt) / (invx + Δt)
    C1 = ((k * x) + Δt) / (invx + Δt)
    C2 = (invx - Δt) / (invx + Δt)

    return C0, C1, C2
end

function calc_params(inflow,outflow,dt,init_storage)

    s = calc_storage(inflow,outflow,dt,init_storage)

    o2 = sum(outflow .^ 2)
    si = sum(s*inflow)
end

function calc_storage(inflow,outflow,dt,init_storage)
    len = length(inflow)
    storage = zeros(Float64,len)
    storage[1] = init_storage

    # Δt = (0.5 * dt)

    for i in 1:(len-1)
        storage[i+1] = storage[i] + (dt * (inflow[i+1] + inflow[i]))/2 - (dt* (outflow[i+1] + outflow[i]))/2
    end

    return storage
end

end # module


text:
using GLPlot, GLAbstraction, GeometryTypes, Colors; GLPlot.init()
using GLWindow, Reactive, ModernGL, GLVisualize

f(x,y,a,b,c) = sin(x * a) ./ b^cos(y / c)

function init(n, a, b, c)
    x = linspace(-4f0, 4f0, n)
    # map the slider signals and great a matrix
    # this foldp is geared towards performance, which is why it mutates v0 in place
    # it's also fairly performant to do this:
    # map((a,b,c)-> [f(x,y,a,b,c) for x=x, y=x], a,b,c)
    v0 = Float32[f(x,y,value(a),value(b),value(c)) for x=x, y=x]
    surface = foldp(v0, a,b,c) do v0, a,b,c
        @inbounds for i = 1:n, j = 1:n
            v0[i,j] = f(x[i], x[j], a, b, c)
        end
        v0
    end;
    # plots surface
    surface_robj = glplot(
        surface, :surface,
        ranges=(x,x), boundinbox=nothing # doesn't calculate boundingbox, which is faster
    ).children[]
    #plot the grid, starting at -4,-4,-2, with width 8,8,5
    glplot(AABB(Vec3f0(-4,-4,-2), Vec3f0(8,8,5)), :grid)
    # Now we draw the controle lines. This should be available as a widget in the Future!
    # allocate an array for point position (performance optimization as well)
    pos_tmp = Point3f0[0]
    w = GLPlot.viewing_screen()
    m2id = mouse2id(w)
    pin_plot = doubleclick(w.inputs[:mouse_buttons_pressed], 0.1)
    index = droprepeats(foldp(((0,0), false), m2id, pin_plot) do v0, m2id, pin
        idx, waspinned = v0
        waspinned && !pin && return v0
        if m2id.id == surface_robj.id
            if m2id.index >= 1 && m2id.index <= n*n
                return ind2sub((n,n), m2id.index), pin
            end
        end
        idx, pin
    end)
    p_l_position = map(surface, index) do data, ij_pinned
        ij, _ = ij_pinned
        if ij != (0,0)
            slicex = Point3f0[(x[_x], x[ij[2]], data[_x, ij[2]]) for _x=1:n]
            slicey = Point3f0[(x[ij[1]], x[y], data[ij[1], y]) for y=1:n]
            pos_tmp[1] = (x[ij[1]], x[ij[2]], data[ij...])
            return vcat(slicex, Point3f0(Inf), slicey), pos_tmp
        end
        Point3f0[], pos_tmp
    end
    linepos = map(first, p_l_position)
    _view(visualize(
        linepos, :lines,
        prerender = ()->glDisable(GL_DEPTH_TEST), # draw over other items
        postrender = ()->glEnable(GL_DEPTH_TEST)
    ), GLPlot.viewing_screen(), camera = :perspective)
    point = map(last, p_l_position)
    _view(visualize(
        (Circle(Point2f0(0), 0.05f0), point),
        color = RGBA{Float32}(0.99, 0, 0.1),
        billboard = true,
        prerender=()-> glDisable(GL_DEPTH_TEST), # draw over other items
        postrender=()-> glEnable(GL_DEPTH_TEST)
    ), GLPlot.viewing_screen(), camera=:perspective)
    text = map(point) do pa
        p = pa[] # is an array
        _x = @sprintf("% 0.3f", p[1])
        _y = @sprintf("% 0.3f", p[2])
        _z = @sprintf("% 0.3f", p[3])
        "x:$_x,y:$_y,z: $_z"
    end
    vis = visualize(text, color=RGBA{Float32}(0.7,0.7,0.7))
    add_widget!(vis)
end

# Create sliders for the surface
a = GLPlot.play_widget(linspace(-4f0, 4f0, 50));
b = GLPlot.play_widget(linspace(1.5f0, 6f0, 50));
c = GLPlot.play_widget(linspace(-4f0, 4f0, 50));
# create surface and controle points/lines
init(100, a, b, c)


text:
using NetCDF: nc_close
include("../src/io/util.jl")
include("../src/io/args.jl")
include("../src/io/netcdf.jl")

using Test
using NetCDF
using Base
using Base.Filesystem

@testset "IO" begin
    workdir = pwd()
    if endswith(workdir, "test")
        cd("..")
    end

    @testset "NetCDF" begin
        filename = tempname(tempdir(); cleanup=true)

        x_vals = -5:.5:5
        y_vals = -3:.25:3
        t_vals = 0:.1:2

        stat_maps = [Dict("b"=>"Bathy")]
        dyn_maps = [Dict("W"=>"BtmDisp", "w"=>"BtmVel"), Dict("W"=>"TopDisp", "u"=>"TopU", "v"=>"TopV", "w"=>"TopW")]

        nc = NC.create_netcdf(filename, x_vals, y_vals, t_vals, stat_maps, dyn_maps)

        nc_vars = keys(nc.vars)
        expected_keys = []
        for mapping ∈ [stat_maps; dyn_maps]
            expected_keys = [expected_keys; collect(values(mapping))]
        end

        @test all(varname -> varname in nc_vars, expected_keys)
        @test nc["x"] == x_vals
        @test nc["y"] == y_vals
        @test nc["time"] == t_vals

        nc = nothing # Finalizer closes file
        nc = NC.get_netcdf(filename)
        nc_vars = keys(nc.vars)
        @test all(varname -> varname in nc_vars, expected_keys)
    end
end

max_stars_repo_name: SeisSol/SAMPLER

max_issues_repo_name: SeisSol/SAMPLER

max_forks_repo_name: SeisSol/SAMPLER


text:
"""
    UnsafePointers

Provides the [`UnsafePtr`](@ref) type, providing convenient (but unsafe) pointer semantics.
"""
module UnsafePointers

export UnsafePtr

"""
    UnsafePtr{T}(r)

A pointer to the contents of `r` which may be a `Ptr`, `Ref`, `Array`, `String` or anything with a `pointer(r)` method.

`T` specifies the element type and is optional.

It has convenient (but unsafe) semantics:
* `p[]` dereferences the element, and can be assigned to.
* `p[i]` dereferences the `i`th element, assuming the pointer points to an array.
* `p.name` is an `UnsafePtr` to the `name` field of `p[]`. For tuples, `p._n` refers to the `n`th field.
* `p+i` is an `UnsafePtr` to the `i`th next element. `(p+i-1)[]` and `p[i]` are equivalent.
* `p-q` is the number of elements between `p` and `q`, so that `p === q+(p-q)`.
* Iteration yields `p[1]`, `p[2]`, ... forever.
* `Array(p, dims...)` is an array view of contiguous data pointed to by `p` (equivalent to `unsafe_wrap(Array, pointer(p), dims)`).
* `p[idxs]`/`view(p, idxs)` is an array/view of the `i`th element for each `i ∈ idxs`.
* `String(p, [length])` converts `p` to a string (equivalent to `unsafe_string(pointer(p), length)`).

The first four operations have these C equivalents: `*p`, `p[i-1]`, `&(p->name)` and `p+i`.

If the result of dereferencing is pointer-like then an `UnsafePtr` is returned instead (see [`doautowrap`](@ref)). Use `p[!,i]` or `unsafe_load(p,i)` to get the original value.

# Safety

It is the caller's responsibility to ensure that the pointer remains valid, e.g. by ensuring that `r` is not garbage collected.

You will likely crash Julia if you assign to a non-bitstype value.

# Example

Here we access and modify the individual fields of a (mutable) reference to a (immutable) named tuple.

```julia
r = Ref((a=1, b=(2, 3)))
@show r[]            # (a = 1, b = (2, 3))
p = UnsafePtr(r)
p.a[] = 99
p.b._2[] *= 10
@show r[]            # (a = 99, b = (2, 30))
@show Array(p.a, 3)  # [99, 2, 30]
```
"""
struct UnsafePtr{T} <: Ref{T}
    ptr :: Ptr{T}
    UnsafePtr{T}(p::Ptr{T}) where {T} = new{T}(p)
end

function Base.show(io::IO, p::UnsafePtr)
    show(io, typeof(p))
    print(io, " @")
    show(io, convert(Integer, pointer(p)))
end

Base.pointer(p::UnsafePtr) = getfield(p, :ptr)

# convert UnsafePtr -> Ptr
Base.convert(P::Type{<:Ptr}, p::UnsafePtr) = convert(P, pointer(p))
(P::Type{<:Ptr})(p::UnsafePtr) = P(pointer(p))

# convert Ptr -> UnsafePtr
Base.convert(::Type{UnsafePtr}, p::Ptr) = UnsafePtr(p)
Base.convert(::Type{UnsafePtr{T}}, p::Ptr) where {T} = UnsafePtr{T}(p)
Base.convert(::Type{UnsafePtr}, p::UnsafePtr) = p
Base.convert(::Type{UnsafePtr{T}}, p::UnsafePtr{T}) where {T} = p
Base.convert(::Type{UnsafePtr}, p) = convert(UnsafePtr, convert(Ptr, p))
Base.convert(::Type{UnsafePtr{T}}, p) where {T} = convert(UnsafePtr{T}, convert(Ptr{T}, p))

Base.unsafe_convert(::Type{P}, p::UnsafePtr) where {P<:Ptr} = Base.unsafe_convert(P, pointer(p))
Base.unsafe_convert(::Type{Ptr{T}}, p::UnsafePtr) where {T} = Base.unsafe_convert(Ptr{T}, pointer(p))

UnsafePtr{T}(p::Ptr) where {T} = UnsafePtr{T}(Ptr{T}(p))
UnsafePtr{T}(p::UnsafePtr) where {T} = UnsafePtr{T}(pointer(p))
UnsafePtr{T}(p) where {T} = UnsafePtr{T}(UnsafePtr(p))

UnsafePtr(p::UnsafePtr) = p
UnsafePtr(p::Ptr{T}) where {T} = UnsafePtr{T}(p)
UnsafePtr(r::Ref{T}) where {T} = UnsafePtr(Base.unsafe_convert(Ptr{T}, r))
UnsafePtr(x) = UnsafePtr(pointer(x))

Base.unsafe_load(p::UnsafePtr, i::Integer=1) =
    unsafe_load(pointer(p), i)

Base.unsafe_store!(p::UnsafePtr, x, i::Integer=1) =
    unsafe_store!(pointer(p), x, i)

"""
    doautowrap(T)

True if `p[]` should return a `UnsafePtr` whenever `p::UnsafePtr{T}`.
"""
doautowrap(::Type) = false
doautowrap(::Type{<:Ptr}) = true
doautowrap(::Type{Cstring}) = true
doautowrap(::Type{Cwstring}) = true

Base.getindex(p::UnsafePtr{T}, i::Integer=1) where {T} =
    doautowrap(T) ? UnsafePtr(unsafe_load(p, i)) : unsafe_load(p, i)

Base.getindex(p::UnsafePtr, ::typeof(!), i::Integer=1) =
    unsafe_load(p, i)

Base.getindex(p::UnsafePtr, idxs::AbstractArray{<:Integer}) =
    [getindex(p, i) for i in idxs]

Base.getindex(p::UnsafePtr, ::typeof(!), idxs::AbstractArray{<:Integer}) =
    [getindex(p, !, i) for i in idxs]

Base.getindex(p::UnsafePtr, idxs::AbstractVector{Bool}) =
    [getindex(p, i) for (i,b) in enumerate(idxs) if b]

Base.getindex(p::UnsafePtr, ::typeof(!), idxs::AbstractVector{Bool}) =
    [getindex(p, !, i) for (i,b) in enumerate(idxs) if b]

Base.setindex!(p::UnsafePtr{T}, x, i::Integer=1) where {T} =
    unsafe_store!(p, convert(T, x), i)

Base.setindex!(p::UnsafePtr{T}, x, ::typeof(!), i::Integer=1) where {T} =
    unsafe_store!(p, Base.unsafe_convert(T, x), i)

_getproperty(p::UnsafePtr{T}, n::Val) where {T} =
    UnsafePtr{_fieldtype(T, n)}(pointer(p) + _fieldoffset(T, n))

Base.getproperty(p::UnsafePtr, n::Val) = _getproperty(p, n)
Base.getproperty(p::UnsafePtr, n::Symbol) = _getproperty(p, Val(n))
Base.getproperty(p::UnsafePtr, n::Integer) = _getproperty(p, Val(Int(n)))

Base.setproperty!(::UnsafePtr, name::Symbol, ::Any) =
    error("setting properties not supported; maybe you meant `p.$name[] = ...`")
Base.setproperty!(::UnsafePtr, name, ::Any) =
    error("setting properties not supported; maybe you meant `getproperty(p, $(repr(name)))[] = ...`")

Base.propertynames(::UnsafePtr{T}, private::Bool=false) where {T} = fieldnames(T)

Base.iterate(p0::UnsafePtr{T}, p::UnsafePtr{T}=p0) where {T} = p[], p+1

Base.IteratorSize(::Type{<:UnsafePtr}) = Base.IsInfinite()

Base.:+(p::UnsafePtr{T}, o::Integer) where {T} = UnsafePtr(pointer(p) + o*sizeof(T))
Base.:+(o::Integer, p::UnsafePtr{T}) where {T} = UnsafePtr(o*sizeof(T) + pointer(p))

Base.:-(p::UnsafePtr{T}, o::Integer) where {T} = UnsafePtr(pointer(p) - o*sizeof(T))

function Base.:-(p::UnsafePtr{T}, q::UnsafePtr{T}) where {T}
    q, r = fldmod(pointer(p) - pointer(q), sizeof(T))
    r == 0 || error("pointers to T must be a multiple of sizeof(T) apart")
    q
end

Base.:(==)(p::UnsafePtr, q::UnsafePtr) = pointer(p) == pointer(q)
Base.:(==)(p::UnsafePtr, q::Ptr) = pointer(p) == q
Base.:(==)(p::Ptr, q::UnsafePtr) = p == pointer(q)

# Array(p, dims...) = unsafe_wrap(Array, pointer(p), dims)
(::Type{Array{T,N}})(p::UnsafePtr, dims::Vararg{Integer,N}) where {T,N} = unsafe_wrap(Array, Ptr{T}(pointer(p)), dims)
(::Type{Array{T}})(p::UnsafePtr, dims::Vararg{Integer,N}) where {T,N} = Array{T,N}(p, dims...)
(::Type{Array{_T,N} where _T})(p::UnsafePtr{T}, dims::Vararg{Integer,N}) where {T,N} = Array{T,N}(p, dims...)
Base.Array(p::UnsafePtr{T}, dims::Vararg{Integer,N}) where {T,N} = Array{T,N}(p, dims...)
(::Type{A})(p::UnsafePtr, dims::Tuple{Vararg{Integer}}) where {A<:Array} = A(p, dims...)

# view(p, idxs)
Base.view(p::UnsafePtr, i::Integer=1) = Array(p+(i-1))
Base.view(p::UnsafePtr, i::AbstractUnitRange{<:Integer}) = Array(p+(first(i)-1), length(i))
function Base.view(p::UnsafePtr, i::AbstractVector{<:Integer})
    if isempty(i)
        o = 0
        a = Array(p, 0)
    else
        i0, i1 = extrema(i)
        o = i0 - 1
        a = Array(p+o, i1-o)
    end
    view(a, i.-o)
end

# String(p, [length]) = unsafe_string(pointer(p), length)
(::Type{String})(p::UnsafePtr) = unsafe_string(Ptr{UInt8}(pointer(p)))
(::Type{String})(p::UnsafePtr, length::Integer) = unsafe_string(Ptr{UInt8}(pointer(p)), length)

@generated function _fieldindex(::Type{T}, ::Val{i}) where {T,i}
    if i isa Integer
        1 ≤ i ≤ fieldcount(T) || @goto error
        return i
    elseif T <: Tuple && i isa Symbol
        s = string(i)
        startswith(s, "_") || @goto error
        j = tryparse(Int, s[2:end])
        j isa Int || @goto error
        1 ≤ j ≤ fieldcount(T) || @goto error
        return j
    else
        j = findfirst(==(i), fieldnames(T))
        j isa Integer || @goto error
        return j
    end
    @label error
    :(error($("invalid field name $(repr(i)) for $(repr(T))")))
end

@generated _fieldoffset(::Type{T}, ::Val{i}) where {T,i} =
    try
        fieldoffset(T, _fieldindex(T, Val(i)))
    catch err
        :(throw($err))
    end

@generated _fieldtype(::Type{T}, ::Val{i}) where {T,i} =
    try
        fieldtype(T, _fieldindex(T, Val(i)))
    catch err
        :(throw($err))
    end

end # module


text:
"""
    format_angle(parts; delim=':')

Given the `(whole, minutes, seconds)` parts of an angle, will format into a string with the given delimiter(s). These parts can be generated by the `xxx2dms` and `xxx2hms` methods, for sexagesimal and `hour:minute:second` outputs. Multiple delimiters can be given in a tuple or vector placed after their respective values. For more control over formatting, consider using [Printf](https://docs.julialang.org/en/v1/stdlib/Printf/) or a package like [Formatting.jl](https://github.com/JuliaIO/Formatting.jl).

# Examples
```jldoctest
julia> ang = 45.0; # degrees

julia> format_angle(deg2dms(ang))
"45:0:0.0"

julia> format_angle(deg2hms(ang))
"3:0:0.0"

julia> format_angle(rad2hms(1.5), delim=["h", "m", "s"])
"5h43m46.48062470963538s"
```

# See also
[`deg2dms`](@ref), [`deg2hms`](@ref), [`rad2dms`](@ref), [`rad2hms`](@ref), [`ha2dms`](@ref), [`ha2hms`](@ref)
"""
format_angle(angle; delim=':') = format_angle(angle, delim)
format_angle(w, m, s; kwargs...) = format_angle((w, m, s); kwargs...)

function format_angle(angle, delim::Union{<:AbstractString, Char})
    whole, min, sec = angle
    sgn = signbit(whole) ? '-' : ""
    return string(sgn, Int(whole), delim, Int(min), delim, sec)
end

function format_angle(angle, delims::Union{<:AbstractVector, <:Tuple})
    whole, min, sec = angle
    d1, d2, d3 = delims
    sgn = signbit(whole) ? '-' : ""
    return string(sgn, Int(whole), d1, Int(min), d2, sec, d3)
end


text:
######################################################################
# edwards.jl: Edwards models
######################################################################

#export Edwards, isvalid

######################################################################
# Basic methods
######################################################################

"""
Concrete type for (twisted) Edwards curves.
"""
struct Edwards{T<:Nemo.RingElem} <: EllipticCurve{T}
    a::T
    d::T
end

Edwards(d) = Edwards(one(parent(d)), d)

function base_ring(E::Edwards)
	return Nemo.parent(E.a)
end

"""
Get a description of a Edwards curve.
"""
function show(io::IO, E::Edwards{T}) where T
	coef = (E.c)^2
    print(io, "Elliptic Curve in Edwards form $E.a x² + y² = (1 + $E.d x²y²)  over ")
    show(io, base_ring(E))
end

function projective_equation(E::Edwards)
	K = base_ring(E)
	A, (X, Y, Z) = PolynomialRing(K, ["X", "Y", "Z"])
	return E.a * X^2 * Z^2 + Y^2 * Z^2 - (1 + E.d * X^2 * Y^2)
end

function equation(E::Edwards)
	K = base_ring(E)
	A, (X, Y) = PolynomialRing(K, ["X", "Y"])
	return E.a * X^2 + Y^2 - (1 + E.D * X^2 * Y^2)
end

function isvalid(E::Edwards)
	return (E.a =! 0) & (E.d != 0) & (E.a != E.d)
end


text:
module MovingWindow

using Reexport
using ArrowMacros
@reexport using TimeParallel
using RecipesBase

include("abstract.jl")
include("mowa/budget.jl")
include("mowa/adaptive.jl")
include("mowa/constructor.jl")
include("mowa/solution.jl")
include("mowa/solve.jl")
include("solve.jl")
include("plot.jl")

export MovingWindowSolver
export MovingWindowSolution
export MoWA

export Budget

end


text:
@testset "AbstractDemoCard" begin
    for cardtype in ("markdown", "julia")
        cd(joinpath("assets", "card", cardtype)) do
            include("$(cardtype).jl")
        end
    end
end


text:
using Documenter, Liblet

makedocs(
    modules = [Liblet],
    clean = false,
    sitename="Liblet.jl",
    repo = "https://github.com/manuelelucchi/Liblet.jl.git",
    authors = "Manuele Lucchi",
    pages = [
        "Home" => "index.md",
        "Getting Started" => "gettingstarted.md",
        "Manual" => [
            "Productions" => "man/production.md",
            "Grammar" => "man/grammar.md",
            "Derivation" => "man/derivation.md",
            "Transition" => "man/transition.md",
            "Automaton" => "man/automaton.md"
        ],
        "Tutorials" => [
            "Grammars & Derivations" => "tutorials/derivations.md",
            "Automatons" => "tutorials/automatons.md"
        ]
    ]
)

deploydocs(
    repo = "github.com/manuelelucchi/Liblet.jl.git",
    branch="gh-pages"
)


text:
#=
Copyright (c) 2015, Intel Corporation

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of Intel Corporation nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=#

include("../../../src/Sparso.jl")
using Sparso

function mylogsumexp(b)
  # does logsumexp across column
  log(1 + exp(-abs(b)))+max(b,0)
end

function LogisticLoss(w, X, Xt, y, lambda)
  Xw = X*w
  yXw = y.*Xw
  m = size(X, 1)

  s = sum(mylogsumexp(-yXw))
  fk = s/m + (lambda/2)*norm(w)^2
  gk = -(Xt*(y./(1+exp(yXw))))/m + lambda*w

  fk, gk
end

function lbfgs_ref(X, y, lambda, xinit, tol, k)
  #@printf("%5s%10s%10s%10s\n", "Iter", "alpha", "|dfk|", "fk")

  Sparso.reset_spmp_spmv_time()
  Sparso.reset_knob_spmv_time()

  bytes = (nnz(X)*12. + (size(X,1) + size(X,2))*8)

  spmv_time = 0
  log_time = 0
  direction_time = 0

  m, n = size(X)
  x = xinit
  Xt = X'

  spmv_count = 0

  S = zeros(n, k)
  Y = zeros(n, k)

  a = zeros(k, 1)

  temp  = Array(Cdouble, m)
  Xw    = Array(Cdouble, m)
  yXw   = Array(Cdouble, m)
  dfk   = Array(Cdouble, n)
  dfkp1 = Array(Cdouble, n)
  w     = Array(Cdouble, n)
  dx    = Array(Cdouble, n)

  t0 = time()
  it = 1
  for it=1:100
    set_matrix_property(Dict(
        :temp  => SA_HAS_DEDICATED_MEMORY,
        :Xw    => SA_HAS_DEDICATED_MEMORY,
        :yXw   => SA_HAS_DEDICATED_MEMORY,
        :dfk   => SA_HAS_DEDICATED_MEMORY,
        :dfkp1 => SA_HAS_DEDICATED_MEMORY,
        :w     => SA_HAS_DEDICATED_MEMORY,
        :dx    => SA_HAS_DEDICATED_MEMORY,
      )
    )

    spmv_time -= time()
    Xw = X*x
    spmv_count += 1
    spmv_time += time()

    yXw = y.*Xw

    log_time -= time()
    #s = sum(log(1 + exp(-abs(yXw))) - min(yXw, 0))
    #fk0 = s/m+(lambda/2)*norm(x)^2
    fk0 = Sparso.lbfgs_loss_function1(yXw, x, lambda)
    #temp = y./(1+exp(yXw))
    Sparso.lbfgs_loss_function2!(temp, y, yXw)
    log_time += time()

    spmv_time -= time()
    dfk = -(Xt*temp)/m + lambda*x
    spmv_count += 1
    spmv_time += time()

    if (norm(dfk) < tol)
      break;
    end

    # Since Julia code for this is too slow we do this in C to show sufficient speedups by
    # faster SpMV. To be fair, the reference code also uses it.
    direction_time -= time()
    Sparso.lbfgs_compute_direction!(dx, k, it, n, S, Y, dfk)
    direction_time += time()

    # backtracking line search using armijo criterion
    alphaMax = 1 # this is the maximum step length
    alpha = alphaMax
    rho = 1/2 # < 1 reduction factor of alpha
    c_1 = 1e-4

    while true
      # logistic loss objective funcion
      w = x - alpha*dfk

      spmv_time -= time()
      Xw = X*w
      spmv_count += 1
      spmv_time += time()
      yXw = y.*Xw

      log_time -= time()
      #s = sum(log(1 + exp(-abs(yXw))) - min(yXw, 0))
      #fk = s/m+(lambda/2)*norm(w)^2
      fk = Sparso.lbfgs_loss_function1(yXw, w, lambda)
      log_time += time()
      # end objective function

      if (fk <= fk0 - c_1*alpha*norm(dfk)^2)
        break
      end

      alpha = rho*alpha

      if alpha < 10*eps()
        alpha=0.1
        error("Error in Line search - alpha close to working precision")
      end
    end
    # end backtracking line search using armijo criterion

    #@printf("[%5d]%10.3e%10.3e%10.7f\n", it, alpha, norm(dfk), fk0)

    x = x + alpha*dx

    spmv_time -= time()
    Xw = X*x
    spmv_count += 1
    spmv_time += time()

    yXw = y.*Xw

    log_time -= time()
    Sparso.lbfgs_loss_function2!(temp, y, yXw)
    log_time += time()

    spmv_time -= time()
    dfkp1 = -(Xt*temp)/m + lambda*x
    spmv_count += 1
    spmv_time += time()

    S[:,(it - 1)%k + 1] = alpha*dx
    Y[:,(it - 1)%k + 1] = dfkp1 - dfk
  end

  bytes *= spmv_count
  println("\nSpMV takes $spmv_time sec ($(bytes/spmv_time/1e9) gbps).")

  spmv_time = Sparso.get_spmp_spmv_time()
  if spmv_time > 0
    println("time spent on spmp spmv $spmv_time sec ($(bytes/spmv_time/1e9) gbps)")
    spmv_time = Sparso.get_knob_spmv_time()
    println("time spent on knob spmv $spmv_time sec ($(bytes/spmv_time/1e9) gbps)")
  end

  println("log takes $log_time sec.")
  println("direction takes $direction_time sec.")
  println("lbfgs_ref takes $(time() - t0) sec.")

  x, it
end

filename, fileext = splitext(ARGS[1])
X = matrix_market_read(ARGS[1])
X = SparseMatrixCSC{Float64, Int32}([sparse(ones(size(X, 2), 1)) X'])
y = matrix_market_read(string(filename, "_b", fileext))
y=max(y,0)
(n,p) = size(X)

# Set up problem
lambda = 0
sparsity = nnz(X)/(n*p)
println("n=$n p=$p nnz=$(nnz(X)) stored as sparse=$sparsity")

#Xt=X'

#w, it = GD(X,Xt,y,lambda, zeros(p), 1e-10)
#@printf("Grad-Decent: %d iterations f = %.14f\n", it, LogisticLoss(w,X,Xt,y,lambda)[1])
# Expected output: Grad-Decent: 100 iterations f = 0.34398484995673

if length(ARGS) == 2
  test = ARGS[2]
else
  test = "julia"
end

if test == "julia"
  println("compiler warmup (ignored): ")
  w, it = lbfgs_ref(X, y, lambda, zeros(p), 1e-10, 3)

  println("\nRUN: ")
  w, it = lbfgs_ref(X, y, lambda, zeros(p), 1e-10, 3)
else
  if test == "call-repl"
    set_options(SA_ENABLE, SA_USE_SPMP, SA_REPLACE_CALLS)
  elseif test == "context"
    set_options(SA_ENABLE, SA_USE_SPMP, SA_CONTEXT, SA_REPLACE_CALLS)
  elseif test == "reorder"
    set_options(SA_ENABLE, SA_USE_SPMP, SA_CONTEXT, SA_REORDER, SA_REPLACE_CALLS)
  elseif test == "verbose"
    set_options(SA_ENABLE, SA_USE_SPMP, SA_VERBOSE, SA_CONTEXT, SA_REORDER, SA_REPLACE_CALLS)
  end

  println("compiler warnup (ignored): ")
  xinit, tol, k = zeros(p), 1e-10, 3
  @acc w, it = lbfgs_ref(X, y, lambda, xinit, tol, k)

  println("\nRUN: ")
  xinit, tol, k = zeros(p), 1e-10, 3
  @acc w, it = lbfgs_ref(X, y, lambda, xinit, tol, k)
end

@printf("L-BFGS:      %d iterations f = %.14f\n", it, LogisticLoss(w,X,X',y,lambda)[1])

max_stars_repo_name: IntelLabs/Sparso

max_issues_repo_name: IntelLabs/Sparso

max_forks_repo_name: IntelLabs/Sparso


text:
using LowRankIntegrators
using Test

include("data_agnostic_approximation.jl")
include("data_driven_approximation.jl")