Re: Notazione numerica in Mathematica

#86197
robitex
Amministratore del forum
    Up
    0
    Down
    ::


    Nuova versione che mantiene le righe vuote dell’originale.
    `– convert numeric data
    — eseguire con lua o con texlua

    — restituisce i numeri sulla riga
    local function split(l)
    t = {}
    for w in string.gmatch(l, “[^%s]+”) do
    t[#t+1] = tonumber(w)
    end
    return t
    end

    — carica i dati in una tabella
    local function loadLines(filename)
    local t = {}
    for line in io.lines(filename) do
    local n = split(line)
    if #n > 0 then — mantengo linee vuote
    t[#t+1] = n
    else
    t[#t+1] = {}
    end
    end
    t.dimension = #t
    return t
    end

    — traduce la tabella in terne di numeri
    local function parse(t)
    for x, row in ipairs(t) do
    if #row > 3 then — calcolo della terna su due righe
    newrow = {}
    for i=2,#row do
    k = 1
    if row == 10 then
    newrow[#newrow+1] = row[i-1]*10^t[x-1][k]
    k = k + 1
    else
    newrow[#newrow+1] = row[i-1]
    end
    end
    t[x-1] = nil
    t[x] = newrow
    end
    end
    end

    — salva i dati
    local function save(t, frmt1, frmt2, frmt3)
    — tabs separator formatting string
    f = string.format(“%s\t%s\t%s”, frmt1, frmt2, frmt3)
    — reforming the table
    reform = {}
    for i=1, t.dimension do
    if t then
    if #t == 0 then
    reform[#reform + 1] = “”
    else
    local line = string.format(f, t[1], t[2], t[3])
    reform[#reform + 1] = line
    end
    end
    end
    file = io.open(“dataReformat.txt”,”w”)
    file:write(table.concat(reform, “\n”))
    file:close()
    end

    — execution
    filename = “data.txt”
    rows = loadLines(filename)
    parse(rows)
    save(rows, “%0.6f”, “%0.8f”, “%0.8E”)
    `
    Visto come sono veloci questi script?
    R.

    Go to top