dimecres, 7 de febrer del 2024

Inserir salt de pàgina en codi html

 First page (this will be on page n.1)

<div style="break-after:page"></div>

Second page (This will be on page n.2)

dijous, 28 de desembre del 2023

Gershgorin with montecarlo

 import numpy as np
from itertools import permutations

def is_in_gershgorin_disks(matrix):
    # Check if all eigenvalues of the matrix are in the Gershgorin disks
    eigenvalues = np.linalg.eigvals(matrix)
    
    n = matrix.shape[0]
    for i in range(n):
        center = matrix[i, i]
        radius = np.sum(np.abs(matrix[i, :])) - np.abs(center)
        if not np.all(np.abs(eigenvalues - center) <= radius):
            return False
    return True

def permute_rows(matrix, permutation):
    # Permute rows in the matrix based on the given permutation
    return matrix[list(permutation), :]

def monte_carlo_approximation(matrix, num_samples=1000):
    n = matrix.shape[0]
    
    for _ in range(num_samples):
        # Randomly sample a permutation
        permutation = np.random.permutation(n)
        
        # Permute rows
        permuted_matrix = permute_rows(matrix, permutation)
        
        # Check if the permuted matrix satisfies Gershgorin's Circle Theorem
        if is_in_gershgorin_disks(permuted_matrix):
            return permutation, permuted_matrix
    
    return None, None

# Input matrix
original_matrix = np.array([
    [0, 0, 1],
    [1, 0, 0],
    [0, 1, 0]
])

# Monte Carlo approximation
permutation, permuted_matrix = monte_carlo_approximation(original_matrix, num_samples=1000)

if permutation is not None:
    print(f"\nRows permutation {permutation} results in a matrix satisfying Gershgorin's Circle Theorem:")
    print(permuted_matrix)
else:
    print("No permutation found in the Monte Carlo approximation.")


Gershgorin Circle Theorem

 I want to get the matrice where all ones are next to diagonal.
Seems it could be get from 

import numpy as np
from itertools import permutations
 
def is_in_gershgorin_disks(matrix):
      # Check if all eigenvalues of the matrix are in the Gershgorin disks
      eigenvalues = np.linalg.eigvals(matrix)
   
      n = matrix.shape[0]
      for i in range(n):
        center = matrix[i, i]
        radius = np.sum(np.abs(matrix[i, :])) - np.abs(center)
        if not np.all(np.abs(eigenvalues - center) <= radius):
            return False
      return True
 
def permute_rows(matrix, permutation):
      # Permute rows in the matrix based on the given permutation
      return matrix[list(permutation), :]
 
# Input matrix
original_matrix = np.array([
      [1, 0, 1, 0, 0, 0],
      [0, 0, 0, 1, 0, 0],
      [0, 1, 0, 0, 0, 0],
      [1, 0, 0, 0, 0, 0],
      [0, 0, 0, 1, 0, 1],
      [0, 0, 0, 0, 1, 1]
])
 
# Get all possible row permutations
n = original_matrix.shape[0]
all_permutations = list(permutations(range(n)))
 
best_permutation = None
best_fitness = 0.0  # Initialize with the worst fitness value
 
# Check each permutation for Gershgorin's Circle Theorem
for idx, permutation in enumerate(all_permutations, start=1):
      # Permute rows
      permuted_matrix = permute_rows(original_matrix, permutation)
   
      # Check if the permuted matrix satisfies Gershgorin's Circle Theorem
      if is_in_gershgorin_disks(permuted_matrix):
        # Calculate fitness value based on the sum of eigenvalues
        fitness_value = np.sum(np.linalg.eigvals(permuted_matrix))
       
        # Update the best permutation if the current one is better
        if fitness_value > best_fitness:
            best_permutation = permutation
            best_fitness = fitness_value
 
        print(f"Permutation #{idx} results in a matrix satisfying Gershgorin's Circle Theorem with fitness value: {fitness_value}")
      else:
        print(f"Permutation #{idx} did not result in a matrix satisfying Gershgorin's Circle Theorem.")
 
# Print the best permutation found
print("\nBest Permutation:", best_permutation)
 


dimecres, 27 de desembre del 2023

row sorting

import numpy as np

matrix = np.array([
    [0, 1, 0, 0, 0],
    [0, 0, 0, 1, 0],
    [1, 0, 0, 0, 0],
    [0, 0, 1, 0, 0],
    [0, 0, 0, 0, 1]
])

num_rows = matrix.shape[0]

identity = np.eye(num_rows)

for i in range(len(matrix)):
    for j in range(len(identity)):
        product = matrix[i] * identity[j]
        if np.any(product > 0):
            print(f"{i+1} > {j+1}")


dilluns, 20 de novembre del 2023

al Moodle, llista d'activitats "no ocultes per als estudiants" i el seu enllaç al Clipboard

function copyToClipboard(data) {
    const el = document.createElement('textarea');
    el.value = data.map(row => row.join('\t')).join('\n');
    document.body.appendChild(el);
    el.select();
    document.execCommand('copy');
    document.body.removeChild(el);

    console.log('Data copied to clipboard.');
}

var xpath = '//li[@class and not(.//*[@class="badge badge-pill badge-warning"])]/.//span[@class="inplaceeditable inplaceeditable-text"]/a[@href]';
var elements = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
var linksList = [];

for (var i = 0; i < elements.snapshotLength; i++) {
    var linkElement = elements.snapshotItem(i);
    var linkText = linkElement.textContent.trim();
    var linkHref = linkElement.getAttribute('href');

    // Check if linkText or linkHref contains #
    if (linkText.includes('#') || (linkHref && linkHref.includes('#'))) {
        // Skip this iteration if # is present
        continue;
    }

    linksList.push([linkText, linkHref]);
}

copyToClipboard(linksList);