Skip to content

Instantly share code, notes, and snippets.

@corba777
Created October 24, 2019 15:02
Show Gist options
  • Select an option

  • Save corba777/4318464d645cf8d3830ad2b43269ab38 to your computer and use it in GitHub Desktop.

Select an option

Save corba777/4318464d645cf8d3830ad2b43269ab38 to your computer and use it in GitHub Desktop.
DiffSharp Mergesort.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"kernelspec": {
"name": "ifsharp",
"display_name": "F#",
"language": "fsharp"
},
"language_info": {
"mimetype": "text/x-fsharp",
"nbconvert_exporter": "",
"name": "fsharp",
"pygments_lexer": "",
"version": "4.3.1.0",
"file_extension": ".fs",
"codemirror_mode": ""
},
"language": "fsharp",
"colab": {
"name": "DiffSharp Mergesort.ipynb",
"provenance": [],
"include_colab_link": true
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/corba777/4318464d645cf8d3830ad2b43269ab38/diffsharp-mergesort.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "yZG9Ty6JSb9k",
"colab_type": "code",
"colab": {}
},
"source": [
"#load \"Paket.fsx\""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "3h1vxu_OSb9s",
"colab_type": "code",
"colab": {}
},
"source": [
"let wc = new System.Net.WebClient()\n",
"wc.DownloadFile(\n",
" \"https://github.com/DiffSharp/DiffSharp/releases/download/v0.7.7/DiffSharp-v0.7.7-Linux64.zip\", \n",
" \"/home/nbuser/IfSharp/bin/packages/DiffSharp-v0.7.7-Linux64.zip\")"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "JJi4WzDxSb9v",
"colab_type": "code",
"colab": {}
},
"source": [
"open System.Diagnostics"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "VlcdaxjCSb9z",
"colab_type": "code",
"colab": {}
},
"source": [
"let ps = \n",
" ProcessStartInfo(\n",
" FileName=\"unzip\",\n",
" Arguments=\"/home/nbuser/IfSharp/bin/packages/DiffSharp-v0.7.7-Linux64.zip -d /home/nbuser/IfSharp/bin/packages/DiffSharp-v0.7.7\",\n",
" RedirectStandardOutput=true,UseShellExecute=false)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "iZQmKKJlSb91",
"colab_type": "code",
"colab": {}
},
"source": [
"let p = Process.Start(ps)\n",
"p.StandardOutput.ReadToEnd()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "PKQDQ6vISb96",
"colab_type": "code",
"colab": {}
},
"source": [
"#I \"/home/nbuser/IfSharp/bin/packages/DiffSharp-v0.7.7/\"\n",
"#r \"DiffSharp\"\n",
"#r \"FSharp.Quotations.Evaluator\""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "JEW2UUE2Sb99",
"colab_type": "code",
"colab": {}
},
"source": [
"let openblasFromNuget = @\"/home/nbuser/IfSharp/bin/packages/OpenBLAS/lib/native/bin/x64/\"\n",
"let diffSharpReleaseFromZip = @\"/home/nbuser/IfSharp/bin/packages/DiffSharp-v0.7.7/\""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "txrf5SdZSb-A",
"colab_type": "code",
"colab": {}
},
"source": [
"open System\n",
"Environment.SetEnvironmentVariable(\"LD_LIBRARY_PATH\", Environment.GetEnvironmentVariable(\"LD_LIBRARY_PATH\") + \":\" + diffSharpReleaseFromZip)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "oMA6HMXoSb-C",
"colab_type": "code",
"colab": {}
},
"source": [
"Environment.GetEnvironmentVariable(\"LD_LIBRARY_PATH\")"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "1UvHTNTaSb-G",
"colab_type": "code",
"colab": {}
},
"source": [
"open DiffSharp.AD.Float64"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "pRdtBQ5LSb-J",
"colab_type": "code",
"colab": {}
},
"source": [
"let mergesort (arr :DV)= \n",
" let split (arr : D list) =\n",
" let n = arr.Length\n",
" arr.[0..n/2-1], arr.[n/2..n-1] \n",
"\n",
" let rec merge l r = \n",
" match l, r with\n",
" | a :: ls, b :: rs ->\n",
" if a > b\n",
" then b :: merge l rs\n",
" else a :: merge ls r\n",
" | [], r -> r\n",
" | l, [] -> l \n",
" \n",
" let rec mergeSort (arr :D list) =\n",
" if arr.Length<2 then arr else \n",
" let (x, y) = split arr\n",
" merge (mergeSort x) (mergeSort y) \n",
" \n",
" let lst= arr.ToArray() |> Array.toList\n",
" mergeSort lst |> List.toArray |> toDV"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "l1m5IK6QSb-N",
"colab_type": "code",
"colab": {}
},
"source": [
"let rnd=System.Random()\n",
"let test= Array.init 5 (fun _-> rnd.NextDouble()) |> toDV"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "WpMtpOO8Sb-R",
"colab_type": "code",
"colab": {},
"outputId": "6120da48-e93d-41e2-cbe3-6870d779c517"
},
"source": [
"mergesort test"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"DV [|0.01329147956; 0.1320147026; 0.7009875526; 0.8122346009; 0.9483945975|]"
]
},
"metadata": {
"tags": []
},
"execution_count": 61
}
]
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "aQ1YzN4rSb-U",
"colab_type": "code",
"colab": {},
"outputId": "bd2a8a05-9a63-4a3c-822c-de1394363c33"
},
"source": [
"let jacobianmergesort=jacobian mergesort\n",
"jacobianmergesort test"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"DM [[0.0; 0.0; 0.0; 1.0; 0.0]\n",
" [0.0; 0.0; 0.0; 0.0; 1.0]\n",
" [0.0; 0.0; 1.0; 0.0; 0.0]\n",
" [1.0; 0.0; 0.0; 0.0; 0.0]\n",
" [0.0; 1.0; 0.0; 0.0; 0.0]]"
]
},
"metadata": {
"tags": []
},
"execution_count": 62
}
]
},
{
"cell_type": "code",
"metadata": {
"trusted": true,
"id": "fH2XJZ0XSb-Y",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment